Get values from array in php [duplicate] - php

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

Related

How to get data from big Array in PHP [duplicate]

This question already has answers here:
Implode a column of values from a two dimensional array [duplicate]
(3 answers)
Closed 7 months ago.
I have an array that contains player data. This array changes according to the number of players. The array looks like this:
Array
(
[0] => Array
(
[Id] => 0
[Name] => Playername1
[Frags] => -3
[Time] => 339
[TimeF] => 05:39
)
[1] => Array
(
[Id] => 0
[Name] => Playername2
[Frags] => 0
[Time] => 7
[TimeF] => 00:07
)
)
I want to get from this array from each player only the player name [name]. How do I do this? The output should be a string that looks something like this: Playername1, Playername2
I have not found anything on the Internet or on YouTube. The answer is certainly simple and obvious, but I have not found it.
Im using PHP 8.0.13.
$names = implode(',', array_column($arr, 'name'));
echo $names;
array_column: return the values from a single column in the input array.
implode: Join array elements with a string.
try something like this:
$player_names = '';
foreach($your_array as $key => $value){
$player_names .= $value['Name'].', ';
// this should concatenate all the player names
}

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

PHP Array within array within array [duplicate]

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']

Make array keys to start from 0 again in php [duplicate]

This question already has answers here:
Rebase array keys after unsetting elements [duplicate]
(9 answers)
Closed 8 years ago.
I have an array called $Compare, I used the print_r() function in php:
Array ([2] => Broccoli, raw [3] => Candies, butterscotch [4] => Celery, raw [10] => Apricots, raw)
I want it the keys to be in the correct order: 0,1,2,3 not 2,3,4,10!
In the end it should look like this:
Array ([0] => Broccoli, raw [1] => Candies, butterscotch [2] => Celery, raw [3] => Apricots, raw)
Use array_values:
$Compare = array_values($Compare);

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