I am searching for the best way to get specific values in a multidimensional array. My array looks like this. I dont know how many Arrays i get back so i need this in a foreach.
e.g. I want now to return from every array the [event_budget][0] value. How can i achieve this?
Array
(
[0] => Array
(
[event_budget] => Array
(
[0] => Bis € 4000
)
[event_date] => Array
(
[0] => 27.10.2019
)
[event_date_timestamp] => Array
(
[0] => 1572134400
)
)
[1] => Array
(
[event_budget] => Array
(
[0] => Bis € 500
)
[event_date] => Array
(
[0] => 29.10.2019
)
[event_date_timestamp] => Array
(
[0] => 1572307200
)
)
)
There are different ways in which you can get your required result.
The most basic :
$finalArray = array();
foreach($result as $key => $value){
$finalArray[] = $value['event_budget'][0];
}
The $finalArray will have your required result. You can also use like #rakesh have mentioned.
array_map('array_shift',array_column($a, 'event_budget'));
It would be great if you have control over the main array. If that is the case you can avoid event_budget to be an array.
If possible, it should be made like this:
[event_budget] => Array
(
[0] => Bis € 500
)
[event_budget] => Bis € 500
Hope it was helpful.
Thanks
You can use array_map with array_column and array_shift
$aa = array_map('array_shift',array_column($a, 'event_budget'));
print_r($aa);
Working example : https://3v4l.org/fKpjY
Start with:
print_r(array_column($array, 'event_budget'));
But as event_budget is array too, you need to get columns with index 0:
print_r(array_column(
array_column($array, 'event_budget'),
0
));
Full demo.
Related
I am working with php and arrays, I have multiple arrays like following
Array
(
[0] => Array
(
[wallet_address] => 0x127e61982701axxxxxxxxxxxxxxxxxxxxxxxxxxx
)
[1] => Array
(
[wallet_address] => 0xf80a41eE97e3xxxxxxxxxxxxxxxxxxxxxxxxxxxx
)
[2] => Array
(
[wallet_address] => 0x24361F1602bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
)
and so on....
And i want to make them in single array with comma like following way
$set = array("0x127e61982701axxxxxxxxxxxxxxxxxxxxxxxxxxx","0xf80a41eE97e3xxxxxxxxxxxxxxxxxxxxxxxxxxxx","0x24361F1602bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
How can i do this ?Here is my current code but not working,showing me same result(0,1,2 keys),Where i am wrong ?
$GetUserFollower; //contaning multiple array value
$set=array();
foreach($GetUserFollower as $arr)
{
$set[]=$arr;
}
echo "<pre>";print_R($set);
The original array is an Assoc array and therefore the wallet_address needs to be addressed specifically in a loop. Or you could use the array_column() builtin function to achieve the same thing.
$GetUserFollower; //contaning multiple array value
$set=array();
foreach($GetUserFollower as $arr)
{
$set[] = $arr['wallet_address'];
}
echo "<pre>";print_r($set);
Or
$new = array_column($GetUserFollower, 'wallet_address');
print_r($new);
RESULT
Array
(
[0] => 0x127e61982701axxxxxxxxxxxxxxxxxxxxxxxxxxx
[1] => 0xf80a41eE97e3xxxxxxxxxxxxxxxxxxxxxxxxxxxx
[2] => 0x24361F1602bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
)
Your comments are making me think you want an array without a key, which is impossible. If you do this with the example you show in your comments
$set = array("0x127e61982701axxxxxxxxxxxxxxxxxxxxxxxxxxx","0xf80a41eE97e3xxxxxxxxxxxxxxxxxxxxxxxxxxxx","0x24361F1602bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
print_r($set);
You will see
Array
(
[0] => 0x127e61982701axxxxxxxxxxxxxxxxxxxxxxxxxxx
[1] => 0xf80a41eE97e3xxxxxxxxxxxxxxxxxxxxxxxxxxxx
[2] => 0x24361F1602bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
)
I have an array with following format.
Array
(
[0] => Array
(
[keyword] => thumbnail_follow
[content] => width
)
[1] => Array
(
[keyword] => thumbnail_resize
[content] => yes
)
)
My desire output is following
Array
(
[thumbnail_follow] => width
[thumbnail_resize] => yes
)
What I have tried so far?
array_shift() but this remove duplicate keys and output become
Array
(
[keyword] => thumbnail_follow
[content] => width
)
array_column I am able to get only single type of values. For example array_column($array, 'keyword')
Array
(
[0] => thumbnail_follow
[1] => thumbnail_resize
)
Using array_column and loop, I can get desire output but there must be some more efficient way to do it but I am known from this
Can you please help me guys?
Using array_column 's third parameter index_key you can do it. Please look at 2nd example of function.
Use it like this
print_r( array_column($array, 'content', 'keyword') );
You should get your desire output by this.
Try this
$newArr = [];
foreach($yourArr as $row) {
$newArr[$row['keyword']] = $row['content'];
}
When I use the code below:
print_r($jsoni);
$badge_url = "http://api.steampowered.com/ISteamUserStats/GetGlobalAchievementPercentagesForApp/v0002/?gameid=841370%3Fkey&steamids=76561198108211948&fbclid=IwAR0B4wUlosbqFElHBJw-AkLwb3mGsv42xKdtrEAarDmD97Ur3AprrkW4tCk";
$jsoni = json_decode(file_get_contents($badge_url), true);
I get the following as a result:
Array
(
[achievementpercentages] => Array
(
[achievements] => Array
(
[0] => Array
(
[name] => GAME_GREEN_LIGHT
[percent] => 70.9000015259
)
[1] => Array
(
[name] => CAREER_EARN_BADGE
[percent] => 62.2999992371
)
How can I get it so that it only shows the name and the percentage?
print_r($jsoni['achievementpercentages']['achievements'])
You could loop through the decoded data to create an associative array with the achievement names as keys and the percentages as values:
$achievements = [];
foreach($jsoni['achievementpercentages']['achievements'] as $achievement)
$achievements[$achievement['name']] = $achievement['percent'];
Which outputs:
Array
(
[GAME_GREEN_LIGHT] => 70.9000015259,
[CAREER_EARN_BADGE] => 62.2999992371
)
So im now using the following code.
<?php
$achievements = [];
foreach($jsoni['achievementpercentages']['achievements'] as $achievement)
$achievements[$achievement['name']] = $achievement['percent'];
print_r($achievements);
?>
wich results in
Array ( [GAME_GREEN_LIGHT] => 70.9000015259 [CAREER_EARN_BADGE] => 62.2999992371
how do i get it to show a list from top to bottom instead of in a cluster of text?
Thanks for the help!
I have a JSON Array
[0] => Array
(
[stage_id] => 80
[yieldVal] => Array
(
[0] => Array
(
[datajson] => [{"name":"doi","value":"215"},{"name":"dateofpollinationstops","value":"Date of Pollination Stops~23-3-2015"}]
)
[1] => Array
(
[datajson] => [{"name":"doi","value":"698"},{"name":"dateofpollinationstops","value":"Date of Pollination Stops~23-3-2015"}]
)
)
)
I need to extract the values from this Array
[0] => Array
(
[stage_id] => 80
[yieldVal] => Array
(
[doi_value] => 215
[doi_value] => 698
)
)
I have tried decoding the JSON. But unable to continue further.
$phpArray = json_decode($res['datajson'], true);
How to extract the values and assign the key.
EDIT : My final output should be
[0] => Array
(
[stage_id] => 80
[yieldVal] => 913 //215+698 -> Extracting values from [datajson]
)
One thing that may of tripped you up is that your datajson string is:
`[{"name":"doi","value":"215"},{"name":"dateofpollinationstops","value":"Date of Pollination Stops~23-3-2015"}]`
The square brackets mean that json_decode will create an array from the objects.
Anyway, try this...should give you the exact output you asked for:
$yieldVal = 0;
foreach ($res['yieldVal'] as $key => $arr) {
$decode = json_decode($arr['datajson']);
$yieldVal = $yieldVal + $decode[0]->value;
}
$newArray = array (
'stage_id' => $res['stage_id'],
'yieldVal' => $yieldVal
);
//var_dump($newArray);
echo "<pre>".print_r($newArray, true)."</pre>";
You should be able to get the value with:
$doi_value = $phpArray[0]['value'];
You can then sum them, push them onto a resulting array, or whatever.
I got this array output in php:
Array
(
[blabla0] => Array
(
[0] => lalala
[1] => lelele
)
[blabla1] => Array
(
[0] => lalala
[1] => lelele
)
)
If I'd like to print by associative name, it's like this: $myArray['blabla0'][0] , and it works. But I want to do like this: echo $myArray[0][0], by index...is there any way?
Mark bakers answer is best for one option. You can convert all of the array with array_values eg
$new_array = array_values($old_array);
echo $new_array[0][0];
It depends how many times you wish to access the array
http://php.net/manual/en/function.array-values.php