This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 3 years ago.
I retrieve a post via REST API as per the following:
$response = wp_remote_get(get_home_url()."2/wp-json/wp/v2/posts?include=".$postId);
$data = json_decode($response['body'], true);
Then I have:
'acf' =>
array (
'descrizione_lavoro' => '
Lorem ipsum dolor
',
'location' =>
array (
'lat' => '41.36256133817761',
'lng' => '2.131976960327165',
),
'categories' =>
array (
0 => 627,
),
'partner' =>
array (
0 => 'Sandra Yannis',
),
'collaboratori' =>
array (
0 => 'Fran White',
1 => 'Sam Jumper',
),
'importo' => '1200000',
'galleria' =>
array (
0 =>
array (
'ID' => 128345,
'id' => 128345,
'title' => 'villa1',
'filename' => 'villa1.jpg',
'filesize' => 78350,
'url' => 'http://example.com/ing2/wp-content/uploads/2019/03/villa1.jpg',
And I need to get url so I do:
var_dump($data[0]['acf']["galleria"][0]['url']);
And works fine, but I have more than one index under galleria therefore if I do:
var_dump($data[0]['acf']["galleria"][1]['url']);
I will get the second image but there could be plenty, how would I loop in order to retrieve all images??
Use
foreach( $data[0]['acf']["galleria"] as $gallery ) {
echo $gallery['url'];
}
This question already has answers here:
Sort multidimensional array by multiple columns
(8 answers)
Closed 4 years ago.
I have below array with me:
Array(
[0] => Array(
['sort'] => 1
['ques'] => 'Zing order'
)
[1] => Array(
['sort'] => 1
['ques'] => 'How stackoverflow works?'
)
[2] => Array(
['sort'] => 2
['ques'] => 'What is PHP'
)
)
What I want is:
Array(
[0] => Array(
['sort'] => 1
['ques'] => 'How stackoverflow works?'
)
[1] => Array(
['sort'] => 1
['ques'] => 'Zing order'
)
[2] => Array(
['sort'] => 2
['ques'] => 'What is PHP'
)
)
Ideally, it should sort first with sort key and then alphabetically with ques key.
You can use array_multisort() with the help of array_column(). So you have the power to set Sort Order now :)
<?php
$array = array(
array(
'sort' => 1,
'ques' => 'Zing order'
),
array(
'sort' => 1,
'ques' => 'How stackoverflow works?'
),
array(
'sort' => 2,
'ques' => 'What is PHP'
)
);
array_multisort(array_column($array, 'sort'), SORT_ASC,
array_column($array, 'ques'), SORT_ASC,
$array);
print_r($array);
?>
DEMO: https://3v4l.org/ofeZG
i had an associative array like this : i need to merge two array and last hr rating i need to add hrrating1 = , hr rating 2 = like this
array (
0 =>
array (
'skill_name' => 'JDK (Java Development Kit)',
'desc' => '',
'req_rating' => '2',
'user_rating' => '3',
'hrRating' => '2',
),
1 =>
array (
'skill_name' => 'Java Servlets',
'desc' => '',
'req_rating' => '4',
'user_rating' => '3',
'hrRating' => '3',
),
2 =>
array (
'skill_name' => 'JDK (Java Development Kit)',
'desc' => '',
'req_rating' => '2',
'user_rating' => '3',
'hrRating' => '2',
),
3 =>
array (
'skill_name' => 'Java Servlets',
'desc' => '',
'req_rating' => '4',
'user_rating' => '3',
'hrRating' => '4',
),
)
Needed Output :
array (
0 =>
array (
'skillName' => 'JDK',
'comments' => '',
'jobRating' => '2',
'userRating' => '3',
'skillGap' => '-1',
'hrRating1' => '7',
'hrRating2' => '2',
),
1 =>
array (
'skillName' => 'Java Servlets',
'comments' =>'',
'jobRating' =>'4',
'userRating' =>'3',
'skillGap' => '1',
'hrRating1' => '2',
'hrRating2' => '3',
),
)
i need to combine both array with last column hrRating should different like hrRating1 & hrrating# like this any help regarding????
Updated (single array input)
I do not know if I can use skill_name as a array key (probably not), hence $tmp_skill, $tmp_index.
function process_collection($data) {
$tmp_skill = [];
$tmp_index = [];
$result = [];
foreach ($data as $k => $item) {
$skill = $item['skill_name'];
echo $skill.'<br>';
$idx = array_search($skill, $tmp_skill);
if ($idx === false) {
//$result[$idx]['_ratings'] = $item['hrRating'];
//continue;
$idx = count($tmp_skill);
$tmp_index[] = $idx;
$tmp_skill[] = $skill;
$result[$idx] = $item;
$result[$idx]['_ratings'] = [];
}
$result[$idx]['_ratings'][] = $item['hrRating'];
}
// conversion array of ratings to individual values 'hrRating1', 'hrRating2', ...
foreach($result as &$item) {
$i = 0;
foreach( $item['_ratings'] as $rate)
$item['hrRating' . (++$i)] = $rate;
unset($item['_ratings']);
unset($item['hrRating']);
}
return $result;
}
$array_2 = max($arra_1,$arra) ;
print_r($array_2);
Array ( [0] => Array ( [skillName] => JDK [comments] => [jobRating] => 2 [userRating] => 3 [skillGap] => -1 [hrRating] => 2 ) [1] => Array ( [skillName] => Java Servlets [comments] => [jobRating] => 4 [userRating] => 3 [skillGap] => 1 [hrRating] => 3 ) )
You can use the array_merge() merge function to combine arrays as following:
$comined_array=array_merge($array1,$array2);
Now the $combined_array will be containing the value of both $array1 and $array2.
What you are looking for is array merge. The function array combine is an array of keys and then values whereas array merge append arrays onto the first.
As mentioned in the other answers, change the array variables to match your own.
$array_1 = ['a'];
$array_2 = ['b'];
$new_array = array_merge($array_1, $array_2);
print_r($new_array);
// ['a', 'b']
Find more array functions here.
You can try any from these alternatives:-
Assuming that both the array's has same elements and keys and named it as $arr1 and $arr2
Alternative 1 :- Make an array of all hrRating and asign it to main array
foreach ($arr1 as $key => $data) {
$arr1[$key]['hrRating'] = array($data['hrRating'], $arr2[$key]['hrRating']);
}
print_r($arr1);
Output :-
Array
(
[0] => Array
(
[skillName] => JDK
[comments] =>
[jobRating] => 2
[userRating] => 3
[skillGap] => -1
[hrRating] => Array
(
[0] => 2
[1] => 7
)
)
[1] => Array
(
[skillName] => Java Servlets
[comments] =>
[jobRating] => 4
[userRating] => 3
[skillGap] => 1
[hrRating] => Array
(
[0] => 3
[1] => 2
)
)
)
Alternative 2 :- Assign every hrRating values from different array with new sequence key
foreach ($arr1 as $key => $data) {
unset($arr1[$key]['hrRating']);
$arr1[$key]['hrRating1'] = $data['hrRating'];
$arr1[$key]['hrRating2'] = $arr2[$key]['hrRating'];
}
print_r($arr1);
Output :-
Array
(
[0] => Array
(
[skillName] => JDK
[comments] =>
[jobRating] => 2
[userRating] => 3
[skillGap] => -1
[hrRating1] => 2
[hrRating2] => 7
)
[1] => Array
(
[skillName] => Java Servlets
[comments] =>
[jobRating] => 4
[userRating] => 3
[skillGap] => 1
[hrRating1] => 3
[hrRating2] => 2
)
)
Just to add to the variety: Here is another way of re-arranging your array. I took the liberty of combining all arrays into a single master array $in. By doing a sequence of foreach calls will get your result fairly quickly.
I also added the feature that I named the individual rating arrays after their skill_name. If you don't like that, just comment out the line below:
// $j=$aa['skillName'];unset($aa['skillName']);
Here is the input data I used for testing:
$in=array(
array (
array (
'skillName' => 'JDK',
'comments' => '',
'jobRating' => '2',
'userRating' => '3',
'skillGap' => '-1',
'hrRating' => '2',
), array (
'skillName' => 'Java Servlets',
'comments' =>'',
'jobRating' =>'4',
'userRating' =>'3',
'skillGap' => '1',
'hrRating' => '3',
),
),
array (
array (
'skillName' => 'JDK',
'comments' => '',
'jobRating' => '2',
'userRating' => '3',
'skillGap' => '-1',
'hrRating' => '7',
), array (
'skillName' => 'Java Servlets',
'comments' =>'',
'jobRating' =>'4',
'userRating' =>'3',
'skillGap' => '1',
'hrRating' => '2',
)
),
array (
array (
'skillName' => 'JDK',
'comments' => 'not bad',
'jobRating' => '3',
'userRating' => '3',
'skillGap' => '-1',
'hrRating' => '4',
), array (
'skillName' => 'Java Servlets',
'comments' =>'',
'jobRating' =>'8',
'userRating' =>'3',
'skillGap' => '1',
'hrRating' => '1',
),
));
And this is the complete code for re-arranging:
foreach ($in as $i => $na) foreach($na as $j => $aa) {
$j=$aa['skillName'];unset($aa['skillName']);
foreach ($aa as $k => $v) $ret[$j][$k][]=$v;
}
print_r($ret);
And this is what I get as output:
Array
(
[JDK] => Array
(
[comments] => Array
(
[0] =>
[1] =>
[2] => not bad
)
[jobRating] => Array
(
[0] => 2
[1] => 2
[2] => 3
)
[userRating] => Array
(
[0] => 3
[1] => 3
[2] => 3
)
[skillGap] => Array
(
[0] => -1
[1] => -1
[2] => -1
)
[hrRating] => Array
(
[0] => 2
[1] => 7
[2] => 4
)
)
[Java Servlets] => Array
(
[comments] => Array
(
[0] =>
[1] =>
[2] =>
)
[jobRating] => Array
(
[0] => 4
[1] => 4
[2] => 8
)
[userRating] => Array
(
[0] => 3
[1] => 3
[2] => 3
)
[skillGap] => Array
(
[0] => 1
[1] => 1
[2] => 1
)
[hrRating] => Array
(
[0] => 3
[1] => 2
[2] => 1
)
)
)
See the demo here: http://rextester.com/WEUQ12234
you can combine arrays in two ways:
$third_array = combine_array($array1,$array2);
and the other way is:
$array = $array1 + $array2;
change the $array1 & $array2 as per your array names.
This question already has answers here:
PHP multidimensional array search by value
(23 answers)
Closed 4 years ago.
I have a multidimensional array like this:
$a=Array
(
Array
(
Array
(
'id' => 1265451,
'num' => 09381554465
),
Array
(
'id' => 1265451,
'num' => 09370777561
),
Array
(
'id' => 1265451,
'num' => 0963665361
),
Array
(
'id' => 1265451,
'num' => 0943256361
),
Array
(
'id' => 1265451,
'num' => 0975956361
),
Array
(
'id' => 1265451,
'num' => 0963516361
),
),
Array
(
Array
(
'id' => 1265451,
'num' => 0133377469
),
Array
(
'id' => 1265451,
'num' => 02156326987
),
Array
(
'id' => 1265451,
'num' => 01399632548
),
),
);
I need to search for a specific number in num and return the associated id. I made two attempts, with no success:
This returns null:
$key = array_search(09370777561, $a);
echo ("**The key is: ". $key);
This returns false:
var_dump(in_array(09370777561, $a));
I expected it to return the id 1265451.
This array contains phone numbers and can be very large.
You can use like this:
$column_name = "num";
$key = array_search('09370777561', array_column($your_array, $column_name));
This question already has answers here:
Sort an array of associative arrays by column value
(23 answers)
How to Sort a Multi-dimensional Array by Value
(16 answers)
Closed 6 years ago.
I want to sort this array based on count in descending order. here is my array
array(
46 =>
array (
'name' => 'HSR Layout',
'url' => 'hsr-layout',
'count' => 2,
),
37 =>
array (
'name' => 'Electronic City',
'url' => 'electronic-city',
'count' => 3,
)
)
If you are using Laravel, which your tag suggests, you can use collections to manipulate arrays like this. For example:
$array = collect($array)->sortBy('count')->reverse()->toArray();
Using array_multisort().
$array = array(
46 =>
array (
'name' => 'HSR Layout',
'url' => 'hsr-layout',
'count' => 2,
),
37 =>
array (
'name' => 'Electronic City',
'url' => 'electronic-city',
'count' => 3,
)
);
$price = array();
foreach ($array as $key => $row)
{
$count[$key] = $row['count'];
}
array_multisort($count, SORT_DESC, $array);
print_r($array);
Program Output
Array
(
[0] => Array
(
[name] => Electronic City
[url] => electronic-city
[count] => 3
)
[1] => Array
(
[name] => HSR Layout
[url] => hsr-layout
[count] => 2
)
)
Live demo : Click Here