Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Replacing a Value within a Multi-Dimensional Array

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Replacing a Value within a Multi-Dimensional Array

Postby alecthorne » Sun Feb 03, 2013 12:44 am

The Joomla ZOO extension places information within a multi-dimensional array named $elements.

I display the array structure with:

$arr = json_decode($elements,true);echo <pre>",print_r($arr,true),"</pre>";


The resulting array is below.

Array
(
[a3f9a1ba-85a2-4142-b0d6-6a85bef30cb5] => Array
(
[0] => Array
(
[value] => Telephone Number
)
)

[e3145090-5017-44a9-bfaa-64602bacf96f] => Array
(
[0] => Array
(
[value] => Fax Number
)
)
)


I need to be able to replace the value associated with a specific identifier ie. replace the ‘Telephone Number’ with another string $newdata, as this is associated with the identifier ‘a3f9a1ba-85a2-4142-b0d6-6a85bef30cb5’.

I am new to this, but my initial attempt was as follows.

$newdata = “ABCD”;
foreach($elements as &$value)
{
$value['a3f9a1ba-85a2-4142-b0d6-6a85bef30cb5'] = $newdata;
}


This seems to have absolutely no effect. Can someone point me in the right direction.

Thanks, Alec
alecthorne
New php-forum User
New php-forum User
 
Posts: 7
Joined: Wed Dec 26, 2012 4:21 am

Re: Replacing a Value within a Multi-Dimensional Array

Postby us2rn4m2 » Sun Feb 03, 2013 4:25 am

Code: Select all
// Option 1
$a['a3f9a1ba-85a2-4142-b0d6-6a85bef30cb5'] = null;
$a['a3f9a1ba-85a2-4142-b0d6-6a85bef30cb5'] = 'ABC';
print_r($a['a3f9a1ba-85a2-4142-b0d6-6a85bef30cb5']);
// Result: ABC

// Option 2
$a['a3f9a1ba-85a2-4142-b0d6-6a85bef30cb5'][0]['value'] = null;
$a['a3f9a1ba-85a2-4142-b0d6-6a85bef30cb5'][0]['value'] = 'ABC';
print_r($a['a3f9a1ba-85a2-4142-b0d6-6a85bef30cb5']);
/**
Result:
Array
(
    [0] => Array
        (
            [value] => ABC
        )

)
*/
us2rn4m2
New php-forum User
New php-forum User
 
Posts: 17
Joined: Thu Aug 09, 2012 1:28 pm

Re: Replacing a Value within a Multi-Dimensional Array

Postby seandisanti » Thu Feb 07, 2013 12:52 pm

You're working with an array of associative arrays stored as a JSON value; hence the json_decode() right there at the beginning that turns your json value into the arrays you're displaying. go head and make your changes like $arr[$index][$key]=$newValue; and assuming you're passing this back, don't forget to json_encode($arr,$flags) beforehand as necessary.
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm


Return to PHP coding => General

Who is online

Users browsing this forum: Google [Bot] and 4 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.