PHP acess value of array using key of 2D array - php

I'm working on a php magento script which have a array variable for store some script urls.
array variable $items['js']
var_dump
array(1) {
[""]=>
array(17) {
["prototype/prototype.js"]=>
string(22) "prototype/prototype.js"
["varien/form.js"]=>
string(14) "varien/form.js"
["mage/translate.js"]=>
string(17) "mage/translate.js"
["mage/cookies.js"]=>
string(15) "mage/cookies.js"
["wyomind/layer/native.history.js"]=>
string(31) "wyomind/layer/native.history.js"
["varien/weee.js"]=>
string(14) "varien/weee.js"
["geissweb/vatvalidation-min.js"]=>
string(29) "geissweb/vatvalidation-min.js"
}
}
I tried to access the "geissweb/vatvalidation-min.js" value like this
$items['js']['geissweb/vatvalidation-min.js']
but it return empty value, is there have a way to get that value without use foreach or for loop. Thank You

Your index is '', shown by...
array(1) {
[""]=>
so you need to use...
$items['js']['']['geissweb/vatvalidation-min.js']

You have your variable $items['js'] as an array of arrays what your looking for without a foreach is this :
$items['js'][0]['geissweb/vatvalidation-min.js'] is not valid
after tests
$items['js'][""]['geissweb/vatvalidation-min.js'] is valid.

Related

Modify data in php codeigniter active record result without changing structure

I've got a codeigniter active record query that uses get('table_name')->result();
The output is below, what i'd like to do is unserialize(base_64_decode) the "venue_opening_hours" string and replace that string in the data structure with the result of the unserialized & base64_decoded data. I know i can array_walk to do this normally, but i don't see how to access that particular bit of data as an object within an array...
Thanks!
array(2) {
[0]=>
object(stdClass)#142 (4) {
["entry_id"]=>
string(2) "15"
["google-id"]=>
string(40) "552e7c08d3b86c14d130ebe43a0ba421d03a60ae"
["venue_opening_hours"]=>
string(148) "YToxOntzOjEzOiJvcGVuaW5nX2hvdXJzIjthOjE6e3M6NzoicGVyaW9kcyI7YToxOntzOjQ6Im9wZW4iO2E6Mjp7czozOiJkYXkiO3M6MToiNSI7czo0OiJ0aW1lIjtzOjQ6IjIzMTUiO319fX0="
["title"]=>
string(18) "Place Name"
}
[1]=>
object(stdClass)#143 (4) {
["entry_id"]=>
string(2) "18"
["google-id"]=>
string(40) "71d9c8e1f64f330637c96d30a0ae15533836a85e"
["venue_opening_hours"]=>
string(972) "YToxOntzOjEzOiJvcGVuaW5nX2hvdXJzIjthOjE6e3M6NzoicGVyaW9kcyI7YToxMDp7aTowO2E6MTp7czo1OiJjbG9zZSI7YToyOntzOjM6ImRheSI7czoxOiIxIjtzOjQ6InRpbWUiO3M6NDoiMjMzMCI7fX1pOjE7YToxOntzOjQ6Im9wZW4iO2E6Mjp7czozOiJkYXkiO3M6MToiMSI7czo0OiJ0aW1lIjtzOjQ6IjIzMTUiO319aToyO2E6MTp7czo1OiJjbG9zZSI7YToyOntzOjM6ImRheSI7czoxOiIxIjtzOjQ6InRpbWUiO3M6NDoiMjMxNSI7fX1pOjM7YToxOntzOjQ6Im9wZW4iO2E6Mjp7czozOiJkYXkiO3M6MToiMSI7czo0OiJ0aW1lIjtzOjQ6IjIzMzAiO319aTo0O2E6MTp7czo1OiJjbG9zZSI7YToyOntzOjM6ImRheSI7czoxOiIyIjtzOjQ6InRpbWUiO3M6NDoiMjMxNSI7fX1pOjU7YToxOntzOjQ6Im9wZW4iO2E6Mjp7czozOiJkYXkiO3M6MToiMiI7czo0OiJ0aW1lIjtzOjQ6IjIzMzAiO319aTo2O2E6MTp7czo1OiJjbG9zZSI7YToyOntzOjM6ImRheSI7czoxOiI0IjtzOjQ6InRpbWUiO3M6NDoiMjMzMCI7fX1pOjc7YToxOntzOjQ6Im9wZW4iO2E6Mjp7czozOiJkYXkiO3M6MToiNCI7czo0OiJ0aW1lIjtzOjQ6IjIzMzAiO319aTo4O2E6MTp7czo1OiJjbG9zZSI7YToyOntzOjM6ImRheSI7czoxOiI1IjtzOjQ6InRpbWUiO3M6NDoiMjM0NSI7fX1pOjk7YToxOntzOjQ6Im9wZW4iO2E6Mjp7czozOiJkYXkiO3M6MToiNSI7czo0OiJ0aW1lIjtzOjQ6IjIzNDUiO319fX19"
["title"]=>
string(24) "Other place name"
}
}
You'd access it using:
$array[0]->venue_opening_hours
in a for loop...
//foreach ($array as &$arrayItem)
//{
foreach ($arrayItem as &$object)
{
// extract and convert it...
//$openinghours = unserialize(base64_decode($object->venue_opening_hours));
// Update it...
$object->venue_opening_hours = $unserialize(base64_decode($object->venue_opening_hours));
}
//}
The &$object is a reference, so the assignment will change the value in the original result set...
Also I forgot to also loop the array ... I think! so added the outer loop :)

Get elements of json_encoded array in javascript

i passed a json_encoded array to javascript. Now i would like to acces that array to get the different elemtnts.
i print it out in the console.log() and i get this array:
array(1) {
[16]=>
array(2) {
[3488]=>
array(1) {
[0]=>
array(2) {
["article_no_internal"]=>
string(6) "999184"
["article_name_internal"]=>
string(29) "Geschenkbox Kerzenschein 2011"
}
}
[2615]=>
array(1) {
[0]=>
array(2) {
["article_no_internal"]=>
string(6) "700469"
["article_name_internal"]=>
string(29) "Hotelscheck RomantischeTagef2"
}
}
}
}
This is about right. How can i access the article_name of the second array, with the ID 2615?
found a related question here reading a jsone object, hope for some better explebation or answer. Thanks.
EDIT:
As it seems i made a mistake, i showed a php var_dump in the console. When i try to show the javascript array in the console i get undefined.
Since JSON means "JavaScript Object Notation" you don't need to do anything to access the object's items.
For example you can access:
jsonObject[2615][0]["article_name_internal"]
if this object is String, use eval to convert the string to a JavaScript object and access the items in the same way with the previous example.
var jsonObject = eval(jsonstring);
jsonObject[2615][0]["article_name_internal"]

Modifying an array inside an array

So I need to modify the array in a memcached key-value pair. I need to remove one of the arrays inside the array. An example of what it looks like:
array(2) { [0]=> array(3) { ["username"]=> string(3) "Bob" ["id"]=> string(5) "14537" ["comment"]=> string(4) "cool"} [1]=> array(3) { ["username"]=> string(3) "Tom" ["id"]=> string(5) "14538" ["comment"]=> string(3) "yes"}}
If I know the values of username, id, and comment, how can I delete it? The generic queston: How can I delete array 0?
Considering the answer of doing a foreach loop, I tried
foreach($memcachedarray as $f){
if ($f['id'] == '14537'){
echo key($f);
}
}
But it spits out username
Edit- Ok
I searched some more and found I need to do this:
foreach($memcachedarray as $key => $f){
if ($f['id'] == '14537'){
echo $key;
}
}
That works!
If the Id's are unique across the system then you could use an associative array to store you data then unset the key, otherwise you would want to use a foreach loop to get the array key, then unset that key and recommit your new array back into memcache.

How do I get a particular value in a multidimensional array?

How do I prase this?
$scope = $facebook->api('/me/permissions','GET');
The result is as follows and I want to get the value of installed:
array(1) { ["data"]=> array(1) { [0]=> array(5) { ["installed"]=> int(1) ["offline_access"]=> int(1) ["email"]=> int(1) ["manage_pages"]=> int(1) ["user_about_me"]=> int(1) } } }
I've tried json_decode($scope, true), $scope['installed'], $scope['data']['installed'] etc. What am I missing?
That is a super nested array - Your $scope['data']['installed'] was close. However, you forgot one layer. It should be $scope['data'][0]['installed']. Note the 0 in there - there is a third level.
Accessing any of the scope will start with $scope['data'][0], so I would assign that to a new var to remove those two layers.
$scope = $scope['data'][0]
Then, all you need is the key for the permission
Try $scope['data'][0]['installed']

PHP var_dump array

array(14) {
[0]=>
string(1) "1"
["id"]=>
string(1) "1"
[1]=>
string(7) "myUserName"
["UserID"]=>
string(7) "myUserName"
[2]=>
string(10) "myPassword"
["passwordID"]=>
string(10) "myPassword"
[3]=>
string(24) "myEmail#domain.com"
["emailAddress"]=>
string(24) "myEmail#domain.com"
[4]=>
string(7) "myFirstName"
["firstName"]=>
string(7) "myFirstName"
[5]=>
string(8) "myLastName"
["lastName"]=>
string(8) "myLastName"
[6]=>
string(1) "1"
["active"]=>
string(1) "1"
}
how do i access the contents of this array using PHP?
the above was a var_dump($info)
It depends on which part of the array you are trying to access.
If you are trying to access a specific item, you can access it by its index ; for instance :
echo $info['passwordID'];
Should give you :
myPassword
(edit after the comment)
For the email address, there is this portion in your var_dump's output :
["emailAddress"]=>
string(24) "myEmail#domain.com"
This indicates that the e-mail address is stored in the array as an element that has the key "emailAddress".
Which means you should be able to get that e-mail address like this :
echo $info['emailAddress'];
And as you also have this portion of text in the var_dump's output :
(About that duplication of data, you should read Pekka's answer, who provides an idea of why your data is in your array twice, with both integers and strings as keys)
[3]=>
string(24) "myEmail#domain.com"
You could also use :
echo $info[3];
(of course, in each of those cases, you could also store this to a variable for futures re-use)
Another solution, if you want to access each item, would be to use some foreach loop ; for instance :
foreach ($info as $key => value) {
echo "Value for key $key is $value <br />";
}
You might want to go through the arrays section of the PHP manual, for more informations.
And, also, the section about array functions.
You can use either the numeric or the associative key:
echo $array[0]; // outputs 1
echo $array["id"]; // outputs 1
I'm guessing this is the result of a mysql_fetch_array() operation, isn't it? You may want to specify whether you want a numeric or associative array using the second parameter to that function.
Example:
$record = mysql_fetch_array($query, MYSQL_ASSOC); // for associative keys only
$record = mysql_fetch_array($query, MYSQL_NUM); // for numeric keys only
The array appears to have both string and numeric keys. You can access the fields using the array index operator []. Supply either the numeric key or the column name:
echo $info['UserID']; // output "myUserName"
echo $info['emailAddress']; // output "myEmail#domain.com"
if $info is the array, then you can echo $info[6] for example.
If you want it as a string then $s=print_r($info,true);
It seems you are doing some thing wrong, as there shouldn't be a need to access array like that, and there other ways to access objects as arrays.

Categories