printing from multidimensional arrays? - php

I have a multi array that looks like this:
$_SESSION['cartItems']['quantity']
How do I print out its values? print_r won't work, unless $_SESSION doesn't support multi-dimensional arrays?

print_r($_SESSION['cartItems']); should work.

you can use var_dump($_SESSION). However, print_r should work. Make sure you are doing print_r($_SESSION), and not trying to print_r your variable that may not exist.

If you want to get the quantity of each card item in the array, you can do this:
$quantities = array_map(create_function('item', "$item['quanitity']"), $_SESSION['cardItems']);
// PHP 5.3
$quantities = array_map(function($item) {
return $item['quanitity'];
}, $_SESSION['cardItems']);

Related

Check if value of an array is present in another array

I am trying to check if value of an array $spam is present in array $get_mail.
I have following code, but it doesnt seem to work or I dont understand it properly.
$spam_exists = !array_diff($spam, $get_mail);
if ($spam_exists !== FALSE) { ... }
Any idea why this doesnt work?
Thank you for any reply.
Use the array_intersect function.
$result = array_intersect($spam, $get_mail);
Which will return the values in both arrays as an array, or an empty array if there are no shared results.
So rather than using !array_diff($X,$Y) you could use !empty(array_intersect($X,$Y)) or simply if(array_intersect($X,$Y))

push an array in to multi dimensional array in php issue

I have multidimensional array like this:
$comp = [{"d":1},{"v":9}];
I need to insert another array
$arr = array("s" => 5 );
I tried the following code
$comp [] = array_push($comp,$arr);
I get the output $comp = [{"d":1},{"v":9},{"s":5},3]
I dont need 3.
So I have tried
$comp [] = json_encode(array_push(json_decode($comp),$arr));
But now it shows error.
Plz help me. I am using angular js with php.
You don't need array_push here. Just assign $arr to $comp[]
$comp[] = $arr;
Actually what you did here, let me explain it to you:
$comp[] - means pushback element.
It works the same way here as array_push($comp,$arr), but that function also returns the position where that push happened.
So you are pushing result of pushing the element as well as element itself :)
what you need is
$comp[] = $arr;
OR
array_push($comp,$arr);

PHP populate single array with no keys

I have an indexed array like this:
$indexed = array(0=>2,1=>7,3=>9)
But i need a single array, without indexes, like this:
$notIndexed = array(2,7,9)
Zend_Form does not accept $indexed as parameter for the function populate() for multi checkboxes but works fine with $notIndexed
How can i dynamically transform $indexed to $notIndexed
Thanx for answers
$notIndexed = array_values($indexed);
Are you serious? Use, array_values($indexed).
http://php.net/manual/en/function.array-values.php
$notindex = array_values($indexed)

In PHP, filter an Array of Associative Arrays

In PHP, are there any inbuilt functions to turn
[{"id":1, "name":"John"}, {"id":2, "name":"Tim"}]
into
[{"id":1}, {"id":2}]
?
I've used JSON to describe the objects above, but that's just a conceptual representation of my array of associative arrays. I don't want to have to loop manually - something short and elegant I can fit on one line would be nice.
One line, using array_map:
$arr = json_decode('[{"id":1, "name":"John"}, {"id":2, "name":"Tim"}]');
$new_arr = array_map(function($el){$ret=array("id"=>$el->id);return $ret;},$arr);
var_dump(json_encode($new_arr));
array_map(function($arr){return $arr[0];}, $array);
This should do it.
Edit As noted by Jonathon Hibbard, you could pass array element by reference, this way you do not to assign result of the function and just use changed old array. The code should then be modified appropriately.
First decode json by json_decode. You will get an array. Then follow this link to remove an index from an associative array. Then again decode it. It should work.
do something like:
$array = json_decode($some_json_string, true);
array_walk($array, function($value, $key) use(&$array) {
if($key == "name") {
unset($array[$key]);
}
});
Edit:
Cthulhu's answer won't get ya there without re-assigning it. Could use it as a reference though (equal to the walk. though if you want to use the map, its a bit better not to reallocate with a brand new array copy and just pass it by reference, then remove the key with an unset within it and move on.)
array_map(function(&$array) { unset($array['name']; }, $array);

php issue accessing array

I have the following code :
$results = $Q->get_posts($args);
foreach ($results as $r) {
print $r['trackArtist'];
}
This is the output :
["SOUL MINORITY"]
["INLAND KNIGHTS"]
["DUKY","LOQUACE"]
My question is, if trackArtist is an array, why can't I run the implode function like this :
$artistString = implode(" , ", $r['trackArtist']);
Thanks
UPDATE :
Yes, it is a string indeed, but from the other side it leaves as an array so I assumed it arrives as an array here also.
There must be some processing done in the back.
Any idea how I can extract the information, for example from :
["DUKY","LOQUACE"]
to get :
DUKY, LOQUACE
Thanks for your time
It's probably a JSON string. You can do this to get the desired result:
$a = json_decode($r['trackArtist']); // turns your string into an array
$artistString = implode(', ', $a); // now you can use implode
It looks like it's not actually an array; it's the string '["DUKY","LOQUACE"]' An array would be printed as Array. You can confirm this with:
var_dump($r['trackArtist']);
To me content of $r['trackArtist'] is NOT an array. Just regular string or object. Instead of print use print_r() or var_dump() to figure this out and then adjust your code to work correctly with the type of object it really is.

Categories