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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.