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));
Related
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);
I have several arrays that I want to check for a repeated value and if the value is found to be repeated in one of the other arrays, then combine both of those arrays together.
I give an example below of 2 arrays that have repeated values.
example: the value in purchase_order_number is the same in both arrays below. They are not unique values. But the values in tracking_number are unique.
I want to check if the value in purchase_order_number is repeated in another array. If the same value is found in another array, then combine both of those arrays into 1 array.
I'm trying to get the value in tracking_number and service combined into a single array when the value in purchase_order_number is the same in 2 or more arrays.
Example arrays below.
not combined
array (
'data' =>
array (
15 =>
array (
'type' => 'Tracking',
'id' => 2830143,
'attributes' =>
array (
'tracking_number' => '1Z5270560360309870',
'service' => 'UPS',
'order_id' => 2606218,
'purchase_order_number' => '7249491',
'recipient_attempts' => 1,
),
),
16 =>
array (
'type' => 'Tracking',
'id' => 2830144,
'attributes' =>
array (
'tracking_number' => '1Z5270560361740866',
'service' => 'UPS',
'order_id' => 2606218,
'purchase_order_number' => '7249491',
'recipient_attempts' => 1,
),
),
),
)
Example given below of how I need to combine the two above arrays.
Combined
array (
'data' =>
array (
16 =>
array (
'type' => 'Tracking',
'id' => 2830144,
'attributes' =>
array (
'tracking' =>
array (
0 =>
array (
'tracking_number' => '1Z5270560360309870',
'service' => 'UPS',
),
1 =>
array (
'tracking_number' => '1Z5270560361740866',
'service' => 'UPS',
),
),
'order_id' => 2606218,
'purchase_order_number' => '7249491',
'recipient_attempts' => 1,
),
),
),
)
Here's a reduce based solution with caveats:
$arr['data'] = array_reduce($arr['data'], function ($out, $item) {
// keys we want to extract
static $tracking_keys = ['tracking_number' => '', 'service' => ''];
// yank tracking keys from attributes
$tracking = array_intersect_key($item['attributes'], $tracking_keys);
$item['attributes'] = array_diff_key($item['attributes'], $tracking_keys);
// insert to new array based on order number
$order_no = $item['attributes']['purchase_order_number'];
if (!isset($out[$order_no])) {
$item['attributes']['tracking'] = [$tracking];
$out[$order_no] = $item;
} else {
array_push($out[$order_no]['attributes']['tracking'], $tracking);
}
return $out;
}, []);
Keys in 'data' are not retained and 'id' is set by the first item.
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
)
This question already has answers here:
Sort an array of associative arrays by column value
(23 answers)
How to Sort a Multi-dimensional Array by Value
(16 answers)
Closed 6 years ago.
I want to sort this array based on count in descending order. here is my array
array(
46 =>
array (
'name' => 'HSR Layout',
'url' => 'hsr-layout',
'count' => 2,
),
37 =>
array (
'name' => 'Electronic City',
'url' => 'electronic-city',
'count' => 3,
)
)
If you are using Laravel, which your tag suggests, you can use collections to manipulate arrays like this. For example:
$array = collect($array)->sortBy('count')->reverse()->toArray();
Using array_multisort().
$array = array(
46 =>
array (
'name' => 'HSR Layout',
'url' => 'hsr-layout',
'count' => 2,
),
37 =>
array (
'name' => 'Electronic City',
'url' => 'electronic-city',
'count' => 3,
)
);
$price = array();
foreach ($array as $key => $row)
{
$count[$key] = $row['count'];
}
array_multisort($count, SORT_DESC, $array);
print_r($array);
Program Output
Array
(
[0] => Array
(
[name] => Electronic City
[url] => electronic-city
[count] => 3
)
[1] => Array
(
[name] => HSR Layout
[url] => hsr-layout
[count] => 2
)
)
Live demo : Click Here
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