PHP - Access array in array [duplicate] - php

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.

Related

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);

How to set array of array in single array in php [duplicate]

This question already has answers here:
How to Flatten a Multidimensional Array?
(31 answers)
Closed 3 years ago.
Following is my array
Array
(
[0] => Array
(
[1] => Beanie
)
[1] => Array
(
[2] => Cap
)
[2] => Array
(
[1] => Beanie with Logo
)
)
I want this into a single array like set key and value of array.
array(
[1]=>Beanie,
[2]=>Cap,
[1]=>Beanie with Logo,
)
Demo Link.
You can do,
array_merge(...$arr);
Splat operator will expose to all the values of array internally, to flatten array.
Your expected output is not possible in any coding language with
arrays.
Note: Indexes are mean to be unique, don't issue over its uniqueness :)

Access Variable Member [duplicate]

This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 5 years ago.
I have an array variable $array like this
$array = Array
(
[results] => stdClass Object
(
[successc] => stdClass Object
(
[926] => stdClass Object
(
[transaction_id] => xx
[transaction_code] => xx
[status] => xx
[amount] => 5
)
)
[success] => Array
(
[0] => Successful transaction
)
)
)
I want to access the transaction_id element. The 926 is a variable value. It could very well be 927 or 928. It comes from another object $cc. Would it be correct to access the transaction_id using the following code?
$x = $cc->id;
$transaction_id = $array['results']->successc->{$x}->transaction_id;
Your approach is not bad, but the code structure looks more like an array.
To convert to a full array, you can encode to json en decode to array.
$array = json_decode(json_encode($array), true);
When TRUE, returned objects will be converted into associative arrays.
So you can access each level of $array as array element.

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);

PHP Randomly select an array from an multidimensional array [duplicate]

This question already has answers here:
Get random item from array [duplicate]
(4 answers)
multi dimensional array in random order
(4 answers)
Closed 9 years ago.
So I have this array of objects. From which I want to take at random one of the objects from the array, and use it for its intended purpose. I have tried array_rand() but that only returned a random value from one of the arrays within. Is there a method similar to array_rand() that will let me use the whole array as the variable rather than a value pluked from within it?
Example Array:
Array
(
[0] => stdClass Object
(
[id] => 10003
[state] => CA
)
[1] => stdClass Object
(
[id] => 10003
[state] => CA
)
[2] => stdClass Object
(
[id] => 10006
[state] => CA
)
)
What I want to do when doing something similar to array_rand() is end up with a variable that is
[0] => stdClass Object
(
[id] => 10006
[state] => CA
)
or similar
From array_rand documentation:
[array_rand] picks one or more random entries out of an array, and returns the
key (or keys) of the random entries.
To summarize: if you want to retrieve a random value from an array, you need to use the random key provided by array_rand to access it.
Solution, assuming your array is stored in $obj:
$random_obj = $obj[array_rand($obj));

Categories