Jump to content

[SOLVED] problems with array_merge


simpli

Recommended Posts

Hi,

I have a starting and I want to concatenate some arrays to it. I use the newly discovered array_merge.

The state of my original array is the following (from print_r)

Array ( [0] => Array ( [scenario_id] => 1 [node_id] => 0 [node_type] => scenario [node_name] => Scenario1 )

[1] => Array ( [scenario_id] => 2 [node_id] => 40 [node_type] => scenario [node_name] => Scenario2 ) )

 

I then proceed to add one array at a time to it ( I am extracting the arrays from a database table). When I print_r that array, I see that it's in a different format than the first one. What Ihave is

Array ( [scenario_id] => 1 [node_id] => 6 [node_type] => cost Center group [node_name] => sac-stc-qt )

Array ( [scenario_id] => 1 [node_id] => 31 [node_type] => Cost center group [node_name] => Ingénierie )

Array ( [scenario_id] => 2 [node_id] => 42 [node_type] => cost Center group [node_name] => New organization )

If you copy the different in a text editor you will be able to see them more clearly and see how they are structurally different. Now that I'm writing this post, as I am formatting the information I can see that the format is probably what's causing my error. Anyway when I merge_array the arrays, I get the following result:

1 0 scenario Scenario1

2 40 scenario Scenario2

2 2 2 2

4 4 4 4

c c c c

N N N N

.

 

The two set of arrays are really rows from the same table and I get the same columns for them both. so I expect to be able to merge. Can anyone tell me why  it's not working?

P.S. I obtain The first set with Zend db's fetchAll($select)->toArray(). The second I obtain through Zend db's  $row->toArray(); One returns a rowset that I convert to array and the second a single row that I also convert to array. How can I add the rows that are returned to the first array (which seems to be an array of array).

Thanks for any help clarifying.

JR

 

Link to comment
https://forums.phpfreaks.com/topic/162137-solved-problems-with-array_merge/
Share on other sites

not sure where you are getting the 4, c, and N in your final output...but by the sounds of it, you don't want to merge the arrays, but rather append them.

 

let's consider the following a "row":

Array ( [scenario_id] => 1 [node_id] => 6 [node_type] => cost Center group [node_name] => sac-stc-qt ) 

in that case, your original array is an array of rows. so, we want to append the new rows to it. If your original array was called $entries and your new rows are $row1, $row2, and $row3, the code would look like:

$entries[] = $row1;
$entries[] = $row2;
$entries[] = $row3;
print_r($entries);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.