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'];
}
Related
I have a custom field fw_options which looks like below. I am trying to extract data such as gender, _books_read, other_skills (especially this) etc but I can't get my head around this type of custom field. I know how to output a custom field using get_post_meta( get_the_ID(), I just don't know how to extract data from this kind of format. I am a complete beginner in PHP but I'm really trying to understand.
My Code
<span>$ <?php echo get_post_meta( get_the_ID(), 'fw_options', true ); ?></span>
Post Meta Key - fw_options
Value (below)
0 =>
array (
'gender' => 'male',
'tag_line' => 'I like reading journals and thriller books,',
'_books_read' => 12,
'address' => '',
'longitude' => '',
'latitude' => '',
'country' =>
array (
0 => 720,
),
'other_skills' =>
array (
0 =>
array (
'other_skill' =>
array (
0 => '833',
),
'value' => '80',
),
1 =>
array (
'other_skill' =>
array (
0 => '507',
),
'value' => '99',
),
2 =>
array (
'other_skill' =>
array (
0 => '1169',
),
'value' => '99',
),
),
'current_books' =>
array (
),
'awards' =>
array (
),
'experience_level' =>
array (
),
'education_level' =>
array (
),
'banner_image' =>
array (
),
'resume' =>
array (
),
'tutorials' =>
array (
),
'english_level' => 'fluent',
'reader_type' => 'medium',
),
)
I am creating an routing application and get the result as an json array. After transforming it into an php array i get the whole distance and whole duration correctly. Now i need for every value in the key "legs" the distances and durations too but all i did to get the data doesnt work.
The json output of the array looks like this:
array (
'routes' =>
array (
0 =>
array (
'legs' =>
array (
0 =>
array (
'summary' => '',
'weight' => 3741.9,
'duration' => 2912.3, // This value is what i want access
'steps' =>
array (
),
'distance' => 21603.1, // This value is what i want access
),
1 =>
array (
'summary' => '',
'weight' => 3642.1,
'duration' => 2777.4, // This value is what i want access
'steps' =>
array (
),
'distance' => 21611.8, // This value is what i want access
),
),
'weight_name' => 'routability',
'weight' => 7384,
'duration' => 5689.700000000001, // This value i can acesss
'distance' => 43214.899999999994, // This value i can acesss too
),
),
'waypoints' =>
array (
0 =>
array (
'hint' => '',
'distance' => 16.78277948979663, // This value is what i want access
'name' => 'Weg',
'location' =>
array (
0 => 11.4623,
1 => 50.7126,
),
),
1 =>
array (
'hint' => '',
'distance' => 16.62835508134535,
'name' => 'Weg',
'location' =>
array (
0 => 12.6069,
1 => 51.5398,
),
),
2 =>
array (
'hint' => '',
'distance' => 16.78277948979663,
'name' => 'Weg',
'location' =>
array (
0 => 12.343,
1 => 51.576,
),
),
),
'code' => 'Ok',
)
The whole distance (43214.8) and whole duration (5689.7) i get by the following code:
foreach($res2['routes'] as $item)
{
$distances = array_push_assoc($distances, $item['distance'], $item['duration']);
}
In order to get the distances and durations i did the following:
foreach($res2['routes']['legs'] as $item)
{
$durations = array_push_assoc($durations , "DUR", $item['duration']);
}
How can i get the distances and durations from "legs"? Why doenst work $res2['routes']['legs']?
Thank you!
Do notice the the "legs" array exists in index 0 of the "routes" array so looping on it will require using $res2['routes'][0]['legs'].
Morever, notice that using array_push_assoc in loop with the same hard-coded key (as "DUR" in your example) will override the key each time so your data gets lost - you better change it to:
foreach($res2['routes'][0]['legs'] as $item) {
$durations[] = $item['duration'];
}
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:
Is there a function to extract a 'column' from an array in PHP?
(15 answers)
Closed 6 years ago.
I have the following array structure
array (
0 =>
array (
'ID' => '1',
'post_title' => 'Hello world!',
),
1 =>
array (
'ID' => '79',
'post_title' => 'ffffffffffff',
),
2 =>
array (
'ID' => '1720',
'post_title' => 'Git primer',
),
)
I will love to convert it to a structure similar to the one below. Is there any php function that can do this? I am trying to avoid repetitive foreach loop.
array (
'1' => 'Hello world!',
'79' => 'ffffffffffff',
'1720' => 'Git primer',
)
Use array_column()to get this.
Array_column() function return all column name you have specify in parameter.
$array=array (
0 =>
array (
'ID' => '1',
'post_title' => 'Hello world!',
),
1 =>
array (
'ID' => '79',
'post_title' => 'ffffffffffff',
),
2 =>
array (
'ID' => '1720',
'post_title' => 'Git primer',
),
)
$new_array = array_column($array, 'post_title', 'ID');
print_r($new_array);
Output:
Array
(
[1] => Hello world!
[79] => ffffffffffff
[1720] => Git primer
)
Here it is:
//Your array
$test = array (
0 =>
array (
'ID' => '1',
'post_title' => 'Hello world!',
),
1 =>
array (
'ID' => '79',
'post_title' => 'ffffffffffff',
),
2 =>
array (
'ID' => '1720',
'post_title' => 'Git primer',
),
);
//Solution:
foreach ($test as $t){
$new_array[$t['ID']] = $t['post_title'];
}
echo "<pre>";
echo print_r($new_array);
die;
You can accomplish this using following
array_column($array,'post_title','ID');
Output
Array
(
[1] => Hello world!
[79] => ffffffffffff
[1720] => Git primer
)
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