Adding Item to An Associative Array

Hi,

Apologies if this is a total n00b question, but I’ve read the PHP manual on arrays and can’t get my brain around this.

If I have an array defined like this

[php]
$MyArr=array(
‘Fish’=>array(‘Fish1’=>‘Cod’,‘Fish2’=>‘Haddock’),
‘Cats’=>array(‘Cat1’=>‘Lion’,‘Cat2’=>‘Tiger’)
);
[/php]

and I loop through it like this

[php]Foreach($MyArr as $Sub){$Sub[‘Weird’]=‘Platypus’;}[/php]

I was expecting to see an item ‘Weird’ with the value ‘Platypus’ added to both the Fish array and the Cats array.

But when I use

[php]print_r($MyArr);[/php]

I just get

[php]Array ( [Fish] => Array ( [Fish1] => Cod [Fish2] => Haddock ) [Cats] => Array ( [Cat1] => Lion [Cat2] => Tiger ) ) [/php]

Can someone help me by explaining why the extra key-item paring hasn’t been added to the array and how I would achieve this?

Thank you.

Here you go, have a try of this:
[php]<?php
// Create an array of arrays.
$MyArr = array(
‘Fish’ => array(‘Cod’, ‘Haddock’),
‘Cats’ => array(‘Lion’, ‘Tiger’)
);

// print the array(s)
print ‘

’;
print_r($MyArr);
print ‘
’;
// output:
Array
(
[Fish] => Array
(
[0] => Cod
[1] => Haddock
)
[Cats] => Array
    (
        [0] => Lion
        [1] => Tiger
    )

)

// create a new array.
$Sub = array(‘Platypus’, ‘Anteater’);

// add new array to the array(s)
$MyArr[‘Weird’] = $Sub;

// print the array(s) with the new addition.
print ‘

’;
print_r($MyArr);
print ‘
’;
// output:
Array
(
[Fish] => Array
(
[0] => Cod
[1] => Haddock
)
[Cats] => Array
    (
        [0] => Lion
        [1] => Tiger
    )

[Weird] => Array
    (
        [0] => Platypus
        [1] => Anteater
    )

)
?>
[/php]

Hope it helps,
Red :wink:

Here’s another way:
[php]<?php
$MyArr = array(
‘Fish’ => array(‘Cod’, ‘Haddock’),
‘Cats’ => array(‘Lion’, ‘Tiger’)
);

// Print the array as is:
echo ‘

Array As Is

’ . print_r($MyArr, 1) . ‘
’;

// Create a new array:
$Sub = array(‘Weird’ => array(‘Platypus’, ‘Anteater’));

// Merge the Arrays:
$MyArr = array_merge($MyArr, $Sub);

// Print the merged array:
echo ‘

Arrays merged

’ . print_r($MyArr, 1) . ‘
’;[/php]

First thing you are doing it wrong. You created an array “$MyArr” and looped through it using foreach. Now in foreach loop you created another array “$Sub” and gave it some value. Remember you are only looping through the original values of “$MyArr” and not modifying it. Therefore, $MyArr your original array will stay the same, you should use print_r or var_dump on $Sub to see the change. Here is the script run it and you will see the effect.

[php]<?php

	$MyArr = array(

			'Fish'=>array('Fish1'=>'Cod','Fish2'=>'Haddock'),
			'Cats'=>array('Cat1'=>'Lion','Cat2'=>'Tiger')
			);

	Foreach($MyArr as $Sub)
	{
		$Sub['Weird']='Platypus';
		
	}

	var_dump($Sub);

?>[/php]

Also note that as foreach is iterating through the $MyArr array, hence, you will only see the values of last iteration + new values when you var_dump the $sub.

Sponsor our Newsletter | Privacy Policy | Terms of Service