0

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:

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

        [1] => stdClass Object
            (
                [email] => [email protected]
                [message] => none
                [image] => 330.jpg
                [fid] => 52
            )

and I want it to become:

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

Does that clarify?

7
  • 1
    What's your question? It seems like a simple for loop should do it, so what have you tried? Commented May 24, 2013 at 0:22
  • As the OP sits, the grouping doesn't make sense, and should NOT be implemented. In first example each object demonstrates an atomic set of data as it should be. Unless there is some logical grouping you are not showing us, you are better off not touching it. Commented May 24, 2013 at 0:23
  • When combining the data what are you combining it by? I can't see in your example why you would combine 1 and 2 together but not put 3 and 4 in that 1 group too. Commented May 24, 2013 at 0:23
  • @kpsuperplane He said he's combining each adjacent pair: 0+1, 2+3, 4+5, etc. Commented May 24, 2013 at 0:24
  • @Mike Purcell It just looks like pagination/chunking to me Commented May 24, 2013 at 0:27

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Barmar, thank you for the help! Your answer looks great, but I'm afraid I am dealing with objects. Sorry I was misleading with what I said about multi-dimensional arrays. I need to combine object 0+1, object 2+3...so forth.
Are these public properties that you can assign directly, or do you have to go through an object API to set them?
Yes, perfect, your second option did the trick. Thank you for the help!
0

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++;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.