【问题标题】:Multiple array to PHP Object [closed]PHP对象的多个数组[关闭]
【发布时间】:2014-06-27 11:40:45
【问题描述】:

我正在努力让我们的一些人弄清楚如何做到这一点。 我有一个这样的数组:

array (size=2)
  0 => 
    array (size=14)
      'NR COMANDA' => string '251729' (length=6)
      'DESCRIERE' => string 'SO_DK35' (length=7)
      'COD ARTICOL' => string '77A.02.03' (length=9)
      'PACK SSCC' => string '012345000168970576' (length=18)
      'PACK GREUTATE' => string '12.04' (length=5)
      'PACK TARE' => string '0.49' (length=4)
      'PACK GROSS' => string '12.53' (length=5)
      'PACK NOMINAL' => string '12.04' (length=5)
      'LOT' => string '4150' (length=4)
      'DATA EXPIRARE' => string '8/30/2014' (length=9)
      'DATA PRODUCTIE' => string '5/30/2014' (length=9)
      'PALET SSCC' => string '212345000011125386' (length=18)
      'PALET TARE' => string '27' (length=2)
      'DATA FIFO' => string '6/30/2014' (length=9)
  1 => 
    array (size=14)
      .....

我需要在 PHP 对象中包含每个值,如下所示:

    object(EntriesList)[5]
  public 'id' => null    
  public 'nr_comanda' => null
  public 'descriere' => null
  public 'cod_articol' => null
  public 'pack_sscc' => null
  public 'pack_greutate' => null
  public 'pack_tare' => null
  public 'pack_gross' => null
  public 'pack_nominal' => null
  public 'lot' => null
  public 'data_expirare' => null
  public 'data_productie' => null
  public 'palet_sscc' => null
  public 'palet_tare' => null
  public 'data_fifo' => null

你能帮我写一下php代码吗?提前致谢。

【问题讨论】:

  • 这里没有代码编写服务 - 包括您的尝试、当前输出以及您看到的任何错误的详细信息
  • 你的问题给了你答案。使用具有该对象所需的所有参数的 EntriesList 创建类名。然后通过 getter/setter 或使用公共分配值给这些变量并在对象中获取类。

标签: php arrays object


【解决方案1】:

为了回答,这里是:

$result = array();
foreach($arrays as $array)
{//your initial array
    $obj = new EntriesList;
    foreach($array as $key=>$data)
    {
        $obj->{str_replace(' ',"_",strtolower($key))} = $data;//convert the key to lower and replace space with underscore

    }
    $result[] = $obj;
} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-18
    • 1970-01-01
    • 2021-12-07
    • 2021-03-09
    • 2021-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多