php: how to get indexed array of key values [duplicate] - php

This question already has answers here:
PHP Store Key Value from Associative Array into Simple Array
(5 answers)
Closed 8 years ago.
Assuming I have an array like
$arr = array (
array( 'foo' => 'Lorem' ),
array( 'foo' => 'ipsum' ),
array( 'foo' => 'dolor' ),
array( 'foo' => 'sit' )
);
How can I quickly convert this array into an indexed array for the key "foo"? So that the result is
Array
(
[0] => 'Lorem'
[1] => 'ipsum'
[2] => 'dolor'
[3] => 'sit'
)
Are there any quick ways with PHP functions? Or do I simply have to create a new array, iterate over the other one and insert the values manually.

You could make use of array_column which is available from PHP 5.5
print_r(array_column($arr,'foo'));
The code...
<?php
$arr = array (
array( 'foo' => 'Lorem' ),
array( 'foo' => 'ipsum' ),
array( 'foo' => 'dolor' ),
array( 'foo' => 'sit' )
);
print_r(array_column($arr,'foo'));
Demo

You can use array_map(). This works -
$new_arr = array_map(function($v){return $v['foo'];}, $arr);
var_dump($new_arr);
// OUTPUT
array
0 => string 'Lorem' (length=5)
1 => string 'ipsum' (length=5)
2 => string 'dolor' (length=5)
3 => string 'sit' (length=3)

.. Or using array_map(..):
<?php
$arr = array (
array( 'foo' => 'Lorem' ),
array( 'foo' => 'ipsum' ),
array( 'foo' => 'dolor' ),
array( 'foo' => 'sit' )
);
print_r(array_map(function($x){return $x["foo"];}, $arr));
Output:
Array
(
[0] => Lorem
[1] => ipsum
[2] => dolor
[3] => sit
)

Related

Reorder multidimensional arrays manually by array key name

I have this multidimensional PHP array:
array (
0 =>
array (
'name_lower' => 'apples',
'name' => 'Apples',
),
1 =>
array (
'name_lower' => 'pears',
'name' => 'Pears',
),
2 =>
array (
'name_lower' => 'avocados',
'name' => 'Avocados',
),
3 =>
array (
'name_lower' => 'bananas',
'name' => 'Bananas',
),
)
What I'm trying to manually reorder the arrays inside the multidimensional array and list them in exact the following order:
array (
0 =>
array (
'name_lower' => 'bananas',
'name' => 'Bananas',
),
1 =>
array (
'name_lower' => 'avocados',
'name' => 'Avocados',
),
2 =>
array (
'name_lower' => 'pears',
'name' => 'Pears',
),
3 =>
array (
'name_lower' => 'apples',
'name' => 'Apples',
),
)
It does not follow a pattern to automatically sort the arrays. It needs to be rearranged manually by name. Any ideas?
If you index the array on something unique and set an array with the sort order with those unique values, then you can map the sort order array and extract from the main array:
$sort = array('bananas', 'avocados', 'pears', 'apples');
$array = array_column($array, null, 'name_lower');
$array = array_map(function($v) use($array) { return $array[$v]; }, $sort);

Searching in a multidimensional php array [duplicate]

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

Combining multidimensional array to single array [duplicate]

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
)

How can I group by a count of a subvalue of an array?

I've been trying every combination of array_count_values and array_map I can think of. I'm hoping you can show me the way.
Here's a sample array:
$arr = array(
array('name' => 'foo','score' => 1),
array('name' => 'foo','score' => 2),
array('name' => 'bar','score' => 1)
);
I'd like to combine the scores of all sub values with the same name. Something like GROUP BY name in MySQL.
Array
(
[0] => Array
(
[name] => foo
[score] => 3
)
[1] => Array
(
[name] => bar
[score] => 1
)
)
I know there are other questions that sort multidimensional arrays, but I'm not able to find one that sums one of the sub values along the way.
You can use array_reduce
$arr = array(
array('name' => 'foo','score' => 1),
array('name' => 'foo','score' => 2),
array('name' => 'bar','score' => 1));
$array = array_reduce($arr, function ($a, $b) {
isset($a[$b['name']]['score']) ? $a[$b['name']]['score'] += $b['score'] : $a[$b['name']] = $b;
return $a;
});
var_dump($array);
Output
array
'foo' =>
array
'name' => string 'foo' (length=3)
'score' => int 3
'bar' =>
array
'name' => string 'bar' (length=3)
'score' => int 1

php getting unique values of a multidimensional array [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
php multi-dimensional array remove duplicate
I have an array like this:
$a = array (
0 => array ( 'value' => 'America', ),
1 => array ( 'value' => 'England', ),
2 => array ( 'value' => 'Australia', ),
3 => array ( 'value' => 'America', ),
4 => array ( 'value' => 'England', ),
5 => array ( 'value' => 'Canada', ),
)
How can I remove the duplicate values so that I get this:
$a = array (
0 => array ( 'value' => 'America', ),
1 => array ( 'value' => 'England', ),
2 => array ( 'value' => 'Australia', ),
4 => array ( 'value' => 'Canada', ),
)
I tried using array_unique, but that doesn't work due to this array being multidimensional, I think.
Edit: I also need this array to be multi-dimensional and in this format, I can't flatten it.
array_unique is using string conversion before comparing the values to find the unique values:
Note: Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In words: when the string representation is the same. The first element will be used.
But an array will always convert to Array:
var_dump("Array" === (string) array());
You can solve this by specifying the SORT_REGULAR mode in the second parameter of array_unique:
$unique = array_unique($a, SORT_REGULAR);
Or, if that doesn’t work, by serializing the arrays before and unserializing it after calling array_unique to find the unique values:
$unique = array_map('unserialize', array_unique(array_map('serialize', $a)));
Here :)
<?php
$a = array (
0 => array ( 'value' => 'America', ),
1 => array ( 'value' => 'England', ),
2 => array ( 'value' => 'Australia', ),
3 => array ( 'value' => 'America', ),
4 => array ( 'value' => 'England', ),
5 => array ( 'value' => 'Canada', ),
);
$tmp = array ();
foreach ($a as $row)
if (!in_array($row,$tmp)) array_push($tmp,$row);
print_r ($tmp);
?>
Use SORT_REGULAR flag.
$unique_array = array_unique($a, SORT_REGULAR);
I'm not sure why it helps but it does. At least with php 5.3

Categories