I have an array like this, i need to get the unique associative index
Array
(
[0] => Array
(
[id] => 200
[name] => james
[place] => ca
)
[1] => Array
(
[id] => 201
[name] => jana
[place] => uk
)
[2] => Array
(
[id] => 203
[name] => jana
[place] => ca
)
)
That means i need to get the unique 'place' from that array like
ca,uk
Make use of array_column() and array_unique()
array_unique(array_column($array, 'place'))
array_column — Return the values from a single column in the input
array (PHP 5 >= 5.5.0, PHP 7)
array_unique — Removes duplicate values from an array (PHP 4 >= 4.0.1, PHP 5, PHP 7)
Related
I have an array, and would like to get all the values like this [*][place],
without duplicate results.
The output should be looking like this :
Sønderjylland
Nordjylland
Sjælland
Array
(
[0] => Array
(
[place] => Sønderjylland
[active] => Lagerarbejde
[num] => 123
)
[1] => Array
(
[place] => Nordjylland
[active] => Tømrer
[num] => 124
)
[2] => Array
(
[place] => Sønderjylland
[active] => Klejnsmed
[num] => 125
)
[3] => Array
(
[place] => Sjælland
[active] => Elektriker
[num] => 126
)
)
You can use array_column, array_unique, array_filter, implode together.
echo implode(' ', array_filter(array_unique(array_column($yourArray, 'place'))));
array_column is supported for (PHP 5 >= 5.5.0, PHP 7)
If you are using older versions then a loop would help.
I have a JSON array which Im trying to parse with PHP using array_column (PHP 5.5).
My objective is to check the value of a particular key in the array and execute some additional code dependent on the result.
For example with the array below...I would like to find field_number 335 in the array and take the value (Last name) and echo to the screen. The actual array [1] could be different each time as the array grows, where as field_number would always be 335.
Array
(
[0] => Array
(
[id] => 286
[lead_id] => 5
[form_id] => 4
[field_number] => 1
[value] => First Name
)
[1] => Array
(
[id] => 287
[lead_id] => 5
[form_id] => 4
[field_number] => 335
[value] => Last name
)
[2] => Array
(
[id] => 288
[lead_id] => 5
[form_id] => 4
[field_number] => 339
[value] => Australia
)
Hopefully that makes sense and with enough information to help someone point me in the right direction.
Many thanks all!
Cheers
you can use array_search && array_column
$key = array_search('335', array_column($array, 'field_number'));
this should give you array id
eg. echo $array[$key]['value'];
Given this array:
Array
(
[0] => Array
(
[title] => this is the newest post
[ssm_featured_post_id] => 70
)
[1] => Array
(
[title] => sdfsfsdf
[ssm_featured_post_id] => 63
)
[2] => Array
(
[title] => test
[ssm_featured_post_id] => 49
)
[3] => Array
(
[title] => Hello world!
[ssm_featured_post_id] => 1
)
)
The ssm_featured_post_id value corresponds to the value of the array items in the second array.
I want to order the first array items in the same order as the items in the second array
Array
(
[1] => 63
[0] => 70
[3] => 1
[2] => 49
)
so the result after sorting would be
Array
(
[0] => Array
(
[title] => sdfsfsdf
[ssm_featured_post_id] => 63
)
[1] => Array
(
[title] => this is the newest post
[ssm_featured_post_id] => 70
)
[2] => Array
(
[title] => Hello world!
[ssm_featured_post_id] => 1
)
[3] => Array
(
[title] => test
[ssm_featured_post_id] => 49
)
)
The simpler way would be to use usort and write a function that uses the second table to compare two values from first table.
You may want to check out array_multisort, particularly the third example given. The idea is that you create arrays based on the "columns" of the multidimensional array, then sort them simultaneously, and put the result back in the original array.
hello i am learning PHP and came upon this multi-level array after using print_r on $this->root
Array (
[0] => 9
[obj] => 3562
[gen] => 0
[1] => Array (
[0] => 5
[1] => Array (
[/AcroForm] => Array (
[0] => 8
[1] => 3563
[2] => 0
)
[/Metadata] => Array (
[0] => 8
[1] => 3559
[2] => 0
)
[/PageLabels] => Array (
[0] => 8
[1] => 3389
[2] => 0
)
[/Pages] => Array (
[0] => 8
[1] => 3392
[2] => 0
)
[/Type] => Array (
[0] => 2
[1] => /Catalog
)
)
)
) Array (
[0] => 9
[obj] => 8
[gen] => 0
[1] => Array (
[0] => 5
[1] => Array (
[/Type] => Array (
[0] => 2
[1] => /Catalog
)
[/Pages] => Array (
[0] => 8
[1] => 1
[2] => 0
)
[/OpenAction] => Array (
[0] => 6
[1] => Array (
[0] => Array (
[0] => 8
[1] => 3
[2] => 0
)
[1] => Array (
[0] => 2
[1] => /FitH
)
[2] => Array (
[0] => 0
)
)
)
[/PageLayout] => Array (
[0] => 2
[1] => /OneColumn
)
)
)
)
i have an question about the behavior of using multi-level arrays, i want to use this function
$pages = $this->pdf_resolve_object($this->c, $this->root[1][1]['/Pages']);
and $this->root[1][1]['/Pages'] which i believe is used to check the array for these keys and if it exists then use as variable for pdf_resolve_object
so my question is 2-fold, one is does $this->root[1][1]['/Pages'] check the array and goes through the keys? if not what is its behavior? and 2 when it checks the array does it go through just the top 4 keys or all of the sub-keys?
If someone can help or link me to some learning material that would be much appreciated, thank you!
1) It does not check for the presence of the array keys -- rather it assumes that those keys already exist and passes the value into the function. If any of the keys did not exist, PHP would issue an E_NOTICE to the effect of Notice: Undefined index: that the key was not found. To check for them would require a call to isset() or array_key_exists() like:
if (isset($this->root[1][1]['/Pages'])) {
$pages = $this->pdf_resolve_object($this->c, $this->root[1][1]['/Pages']);
}
2) There is no need for it to iterate through to find the keys. Knowing the array keys already means they can be accessed directly without iteration. In memory, PHP has stored the array keys and the memory locations of the values they point to. Therefore, with the key alone, PHP can return the value without needing to traverse the array.
There is a lot of good information in the PHP manaul on Arrays
I want to make it so that my multi dimensional array is in a random order. How would you do it?
// This is how the array looks like
print_r($slides);
Array
(
[0] => Array
(
[id] => 7
[status] => 1
[sortorder] => 0
[title] => Pants
)
[1] => Array
(
[id] => 8
[status] => 1
[sortorder] => 0
[title] => Jewels
)
[2] => Array
(
[id] => 9
[status] => 1
[sortorder] => 0
[title] => Birdhouse
)
[3] => Array
(
[id] => 10
[status] => 1
[sortorder] => 0
[title] => Shirt
)
[4] => Array
(
[id] => 11
[status] => 1
[sortorder] => 0
[title] => Phone
)
)
// This how the result is if I use array_rand()
print_r(array_rand($slides, 5));
Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
[4] => 4
)
// This how the result is if I use shuffle()
print_r(shuffle($slides));
1
shuffle() is the way to go here. It prints 1 because shuffle changes the array in-place and returns a boolean, as it is written in the documentation:
Returns TRUE on success or FALSE on failure.
I suggest to also read the documentation of array_rand():
Picks one or more random entries out of an array, and returns the key (or keys) of the random entries.
Always read documentation if you use built-in functions. Don't just assume how the work. I bet it took more time to write the question than looking this up.
Instead of
print_r(shuffle($slides));
do
shuffle($slides);
print_r($slides);
You see shuffle() shuffles the array in-place
i am not sure how you want it to display but you can loop the array and use php rand(0,arraylen) function to parse the array.
It works perfect. print_r(shuffle($slides))) gives the output of TRUE, since the return value of shuffle is a boolean and not an array.
See the working example here: http://codepad.org/B5SlcjGf