使多维数组的值成为多维的关键

时间:2016-11-24 17:01:46

标签: php multidimensional-array

我有两个数组:

Object
    protected 'value' => 
        array (size=4)
          0 => 
            object(stdClass)[668]
              public 'info' => string 'info' (length=10)
              public 'info ' => string 'info' (length=1)
              public 'info' => string 'info' (length=1)
              public 'info' => string 'info' (length=1)
          1 => 
            object(stdClass)[667]
              public 'info' => string 'info' (length=10)
              public 'info' => string 'info' (length=1)
              public 'info' => string 'info' (length=1)
              public 'info' => string 'info' (length=1)
          2 => 
            object(stdClass)[673]
              public 'info' => string 'info' (length=10)
              public 'info' => string 'info' (length=1)
              public 'info' => string 'info' (length=1)
              public 'info' => string 'info' (length=1)
          3 => 
            object(stdClass)[671]
              public 'info' => string 'info' (length=10)
              public 'info' => string 'info' (length=1)
              public 'info' => string 'info' (length=1)
              public 'info' => string 'info' (length=1)

另一个是:

Object
     protected 'value' => 
    array (size=4)
      0 => string '2016-11-23' (length=10)
      1 => string '2016-11-24' (length=10)
      2 => string '2016-11-25' (length=10)
      3 => string '2016-11-26' (length=10)

我想要的是以某种方式合并这些数组,多维数组中的数组的键将等于第二个数组中的值,如下所示:

Object
    protected 'value' => 
        array (size=4)
          2016-11-23 => 
            object(stdClass)[668]
              public 'info' => string 'info' (length=10)
              public 'info ' => string 'info' (length=1)
              public 'info' => string 'info' (length=1)
              public 'info' => string 'info' (length=1)
          2016-11-24 => 
            object(stdClass)[667]
              public 'info' => string 'info' (length=10)
              public 'info' => string 'info' (length=1)
              public 'info' => string 'info' (length=1)
              public 'info' => string 'info' (length=1)
          2016-11-25 => 
            object(stdClass)[673]
              public 'info' => string 'info' (length=10)
              public 'info' => string 'info' (length=1)
              public 'info' => string 'info' (length=1)
              public 'info' => string 'info' (length=1)
          2016-11-26 => 
            object(stdClass)[671]
              public 'info' => string 'info' (length=10)
              public 'info' => string 'info' (length=1)
              public 'info' => string 'info' (length=1)
              public 'info' => string 'info' (length=1)

我该怎么做?

1 个答案:

答案 0 :(得分:1)

您可以使用array_combine。我试图用您的输入数据进行模拟:

$obj = new stdClass;
$obj->p1 = 
$obj->p2 =
$obj->p3 =
$obj->p4 = 'info';

$valueObject = new stdClass;
$valueObject->value = [clone $obj, clone $obj, clone $obj, clone $obj,];

$keyObject = new stdClass;
$keyObject->value = ['2016-11-23','2016-11-24','2016-11-25','2016-11-26',];

$outputObject = new stdClass;
$outputObject->value = array_combine($keyObject->value, $valueObject->value);

var_dump($outputObject);

<强>输出

object(stdClass)#8 (1) {
  ["value"]=>
  array(4) {
    ["2016-11-23"]=>
    object(stdClass)#3 (4) {
      ["p4"]=>
      string(4) "info"
      ["p3"]=>
      string(4) "info"
      ["p2"]=>
      string(4) "info"
      ["p1"]=>
      string(4) "info"
    }
    ["2016-11-24"]=>
    object(stdClass)#4 (4) {
      ["p4"]=>
      string(4) "info"
      ["p3"]=>
      string(4) "info"
      ["p2"]=>
      string(4) "info"
      ["p1"]=>
      string(4) "info"
    }
    ["2016-11-25"]=>
    object(stdClass)#5 (4) {
      ["p4"]=>
      string(4) "info"
      ["p3"]=>
      string(4) "info"
      ["p2"]=>
      string(4) "info"
      ["p1"]=>
      string(4) "info"
    }
    ["2016-11-26"]=>
    object(stdClass)#6 (4) {
      ["p4"]=>
      string(4) "info"
      ["p3"]=>
      string(4) "info"
      ["p2"]=>
      string(4) "info"
      ["p1"]=>
      string(4) "info"
    }
  }
}