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);
Related
I need to merge a php array INTO a php object.
I need my php object to look like this :
{"info1":"value","concurrents":[{"concurrent1":"value"},{"concurent2":"value"}],"info3":"value"};
, I'm actually getting my initial object like this :
$initial = pg_fetch_object($result);
After that, I'm getting the array with this command :
$concurrents = pg_fetch_all($result);
So please, how could i merge $concurrents INSIDE my $initial object, for executing this finally for destination to the front end:
print_r(json_encode($initial));
I've tried a foreach, but it doesn't work.
Have you tried the following?
$initial->concurrents = $concurrents;
Or even:
$initial->concurrents = pg_fetch_all($result);
Don't get your initial result as an object, get it as an array:
$initial = pg_fetch_assoc($result);
Then 'merging' is fairly easy (push the initial on to the beginning of your concurrents array):
$concurrents = pg_fetch_all($result);
array_unshift($concurrents, $initial);
Now you can print them:
print_r(json_encode($concurrents));
I'm working with the Youtube Data Api V3 and this array is beeing returned from my api call:
What i want:
I wanna access the red underlined Array in the Image above and respectively get the latest array inside of it. (In the example i wanna access the [maxres], but it's always different)
What i tried:
$var = end($array)['url']; // $array is my input array
But that doesn't work and so i hope someone can help me :)
You want to use array_pop. Link to PHP documentation.
<?pnp
$var = array_pop($array['modelData:protected']);
$object->modelData is a protected property, you cannot access it. Usually there is a function included in the object to retrieve it.
For example $array = $object->getModelData();
But if it was not protected, this would be a solution:
$array = $object->modelData;
end($array);
$key = key($array);
var_dump($object->modelData[$key];
Try this...
$var = $array['url'][ count($array['url']) - 1 ]
I'm trying to get the first key of an array in an associative array like below. I know I can use key, but I read (on this site), that's it's less efficient.
So I'm using current(array_keys($data)).
Is there another way of doing this? Will I always get the first key when I use current(array_keys($data))? That's what I scared off.
I'm using php 5.3.18. This is the way the script starts off.
<?php
$json = '{"user":"norman","city":"san jose","type":"editor"}';
$data = json_decode($json, true);
echo current(array_keys($data));
//Output I need is "user"
?>
echo current(array_keys($data)); is a long process just use key
echo key($data);
Note
$data = json_decode($json, true); would reset the array ... so no need to call reset again
Try with this code:
reset($data);
$first_key = key($data);
Now on PHP 7.3 >=
$firstKey = array_key_first($data);
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.
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']);