Jump to content

Array Question...?


whit3fir3

Recommended Posts

I am having a problem with an array that I am hoping some one can help me out with.  I have an associative, multidimensional array.  The array has 2 keys named "name" and "value".  After the array is created I am using the array_push function to try and append some values to the end of the array and I can not get it to work for me.  Any example of the code I am trying is below:

 


$myArray = array("name"=>"SomeValue", "value"=>"AnotherValue");

array_push($myArray["name"], "SomeAddedValue");
array_push($myArray["value"], "AnotherAddedValue");

 

What happens is I only see the values in the array that the array was created with.  I never see the new values and no errors are thrown.  Does anyone know of a better way to add values to an associative, multidimensional array.

 

Thanks,

 

whit3fir3

Link to comment
https://forums.phpfreaks.com/topic/183269-array-question/
Share on other sites

That is not a multidimensional array. Neither $myArray["name"] nor $myArray["value"] is an array.

 

Edit: Perhaps you meant something like this:

 

$myArray = Array(
'name' => Array("SomeValue"), 
'value' => Array("AnotherValue")
);

array_push($myArray['name'], 'Added Name');
array_push($myArray['value'], 'Added Value');

echo "<pre>" . print_r($myArray, true) . "</pre>";

 

Output:

Array
(
    [name] => Array
        (
            [0] => SomeValue
            [1] => Added Name
        )

    [value] => Array
        (
            [0] => AnotherValue
            [1] => Added Value
        )

)

Link to comment
https://forums.phpfreaks.com/topic/183269-array-question/#findComment-967282
Share on other sites

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.