简体   繁体   English

PHP数组结合所有其他元素

[英]PHP array combining every other element

I have an object with several properties that creates a multi dimensional array. 我有一个具有多个属性的对象,它们创建了多维数组。 I'm trying to figure out how to create a new array that combines two seperate objects into one. 我试图弄清楚如何创建一个将两个单独的对象组合为一个的新数组。 It would go from this: 它将是这样的:

array(
    object_1(
       'id' => '1',
       'name' => 'joe'
       etc....),
    object_2(
       'id' => '2',
       'name' => 'jessica',
       etc....)
    object_3(
       'id' => '3',
       'name' => 'tim',
       etc....)
    object_4(
       'id' => '4',
       'name' => 'tammy',
       etc....)
    );

And become: 并成为:

array(
    object_1(
        'id' => '1',
        'name' => 'joe',
        etc...
        'id2' = > '2',
        'name2' => 'jessica',
        etc...)
    object_2(
        'id' => '3',
        'name' => 'tim',
        etc...
        'id2' = > '4',
        'name2' => 'tammy',
        etc...)

So, I need to combine the data from alternating elements, and also change the key in all the second objects so it doesn't match the first. 因此,我需要合并来自交替元素的数据,还需要更改所有第二个对象中的键,使其与第一个对象不匹配。 Make sense? 说得通? Sorry if it doesn't, I'll try to clarify if you need! 抱歉,如果没有,我会尽力澄清您是否需要!

Thanks for any help.... 谢谢你的帮助....

EDIT: first two stdclass objects according to print_r: 编辑:根据print_r的前两个stdclass对象:

[results] => Array
    (
        [0] => stdClass Object
            (
                [email] => sample@info.com
                [message] => Create another test
                [image] => 138.png
                [fid] => 53
            )

        [1] => stdClass Object
            (
                [email] => info@sample.com
                [message] => none
                [image] => 330.jpg
                [fid] => 52
            )

and I want it to become: 我希望它成为:

[results] => Array
    (
        [0] => stdClass Object
            (
                [email] => sample@info.com
                [message] => Create another test
                [image] => 138.png
                [fid] => 53
                [email2] => info@sample.com
                [message2] => none
                [image2] => 330.jpg
                [fid2] => 52
            )

Does that clarify? 这澄清了吗?

Assuming it's actually a multi-dimensional array, not an array of objects, this should do it: 假设它实际上是一个多维数组,而不是对象数组,则应这样做:

$new_array = array();
for ($i = 0; $i < count($array); $i +=2) {
  $new_array[] = $array[$i];
  foreach ($array[$i+1] as $key => $value) {
    $new_array[$i/2][$key.'2'] = $value;
  }
}

EDIT: For an array of objects, it becomes: 编辑:对于对象数组,它变为:

$new_array = array();
for ($i = 0; $i < count($array); $i +=2) {
  $new_array[] = $array[$i];
  foreach (get_object_vars($array[$i+1] as $key => $value) {
    $new_array[$i/2]->{$key.'2'} = $value;
  }
}

This will only work for public properties. 这仅适用于公共财产。

You could do something like 你可以做类似的事情

for($i=0;$i<count($array);$i+=2) {
   $id = $array[$i]['id'];
   $name = $array[$i]['name'];
   $id2 = $array[$i+1]['id'];
   $name2 = $array[$i+1]['name'];
}

or 要么

$newarray = array();
$j=0;
for($i=0;$i<count($array);$i+=2) {
    $newarray[$j]['id'] = $array[$i]['id'];
    $newarray[$j]['nae'] = $array[$i]['name'];
    $newarray[$j]['id2'] = $array[$i+1]['id'];
    $newarray[$j]['name2'] = $array[$i+1]['name'];
    $j++;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM