PHP Array within array within array [duplicate] - php

This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 5 years ago.
Hello how do I access the attachments array here, for example how do I return the permalink field:
Array ( [name] => Temp List
[attachments] => Array (
[32107] => Array (
[id] => 32107
[permalink] => /fred
[image] => http://wp-content/uploads/2017/11/f20e4f030a09f2a87740715f15679aa5-3-768x1154.jpg
[thumbnail] => http://wp-content/uploads/2017/11/f20e4f030a09f2a87740715f15679aa5-3-200x300.jpg
[name] => fred
)
)
)
I know there are many questions on arrays but I have tried every way and cant seem to get it.
Many thanks

Try like this
$array['attachments']['32107']['permalink']

Related

how to simplify an array of arrays with single object attribute [duplicate]

This question already has answers here:
how get each single column data from php multidimensional array?
(2 answers)
Closed 2 years ago.
I have the following data structure:
$campaigns =
Array
(
[0] => Array
(
[subject] => cca-cpg
)
[1] => Array
(
[subject] => cleanup-cpg
)
[2] => Array
(
[subject] => gas-cpg
)
[3] => Array
(
[subject] => pollinators-cpg
)
)
what I would like to end up with is:
$campaigns = ['cca-cpg','clean_up-cpg','gas-cpg','pollinators-cpg'];
this will work:
$newCampaigns=[];
for($i=0;$i<count($campaigns);$i++){
array_push($newCampaigns,$campaigns[$i]['subject'];
}
but I was wondering if there's a better way to do this. The data is coming directly from a mysql database
There's array_column() function for you:
$newCampaigns = array_column($campaigns, 'subject');
Source: https://www.php.net/manual/en/function.array-column.php

Get values from array in php [duplicate]

This question already has answers here:
How to echo single value from multidimensional array when key is numeric?
(4 answers)
Closed 3 years ago.
I have output like this:
[0] => Array
(
[voice] => Napisy
[quality] => 720
[source] => YouTube
)
[16] => Array
(
[voice] => Napisy
[quality] => 720
[source] => Mega
)
And I want to get source to get output [Youtube, Mega]. Anyone know how to get this without foreach?
You can get source from whole array into another array using array_column.
your above array is $array
$source = array_column($array, 'source');
print_r($source);

PHP - Access array in array [duplicate]

This question already has answers here:
How can I access an array/object?
(6 answers)
Accessing Arrays inside Arrays In PHP
(4 answers)
Closed 7 years ago.
I am working with response from Youtube Data API and I don't know how to read each values in the array.
Array
(
[0] => youtube#channelListResponse
[1] => "X"
[2] => Array (
[totalResults] => 1
[resultsPerPage] => 1
)
[3] => Array (
[0] => Array
(
[kind] => youtube#channel
[etag] => "XX"
[id] => XXX
)
)
)
When I try to access [id] with echo $array[3]['id'] it returns Notice: Undefined offset: 3
AS Max said you can access single value in array in array by
$arrayName[index]['key']
Example You can access 'kind' with
$arrayName[3]['kind']
Since, inner level array is key value paired you need to specify key instead of index.
If you want to access all value, you can do it with foreach loop and check type of each value use'gettype()' method. if type is array then use same function recursively.

Loop through multiple arrays php [duplicate]

This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 7 years ago.
Can anybody explain me how can i get the value of 'ubicacion' in those arrays, and then, store that value in a php variable.
Array
(
[0] => Array
(
[PC] => Array
(
[ubicacion] => 02021002
)
)
)
Array
(
[0] => Array
(
[PC] => Array
(
[ubicacion] => 034267211
)
)
)
This will give you the value of it..
$value = $array[0]['PC']['ubicacion'];

strip array with only one item in it without looping thru it and not knowing its index [duplicate]

This question already has answers here:
Reset PHP Array Index
(4 answers)
Closed 8 years ago.
Array
(
[2] => stdClass Object
(
[name] => test-song-poll-03
[description] => test-song-poll-03
[created_at] => 2014-05-02T23:19:06Z
[count] => stdClass Object
(
[approved] => 26638
[pending] => 0
[rejected] => 36923
[total] => 63561
)
[tpm] => 47
[approved_tpm] => 9
[pct] => 2
)
)
I have a function that uses array_filter and it returns what you see above. It will only return one object within the array. I do not know what the array index will be, but I know there will only be one item in the array. Is there an array function that strips down the array and just returns the content of it, since I don't need an array with just one item in it.
You should use:
$x = array_values($yourArrayName);
echo $x[0];
You can also use:
echo current($yourArrayName);

Categories