No, there is not a function to get your expected output natively, though you can make your own functions, just use array_column to get the types/column, and then loop over your array, on match remove the item as to not duplicate iterations.
Something like:
<?php
$data = [
['foodType' => 'fruits', 'itemID' => 1, 'itemName' => 'apple'],
['foodType' => 'fruits', 'itemID' => 2, 'itemName' => 'banana'],
['foodType' => 'veggies', 'itemID' => 3, 'itemName' => 'carrot'],
['foodType' => 'veggies', 'itemID' => 4, 'itemName' => 'broccoli']
];
function array_column_multi ($array, $column) {
$types = array_unique(array_column($array, $column));
$return = [];
foreach ($types as $type) {
foreach ($array as
value) {
if ($type === $value[$column]) {
unset($value[$column]);
$return[$type][] = $value;
unset($array[$key]);
}
}
}
return $return;
}
print_r(array_column_multi($data, 'foodType'));
https://3v4l.org/KQVeN
Result:
Array
(
[fruits] => Array
(
[0] => Array
(
[itemID] => 1
[itemName] => apple
)
[1] => Array
(
[itemID] => 2
[itemName] => banana
)
)
[veggies] => Array
(
[0] => Array
(
[itemID] => 3
[itemName] => carrot
)
[1] => Array
(
[itemID] => 4
[itemName] => broccoli
)
)
)
Answer from Lawrence Cherone on Stack OverflowNo, there is not a function to get your expected output natively, though you can make your own functions, just use array_column to get the types/column, and then loop over your array, on match remove the item as to not duplicate iterations.
Something like:
<?php
$data = [
['foodType' => 'fruits', 'itemID' => 1, 'itemName' => 'apple'],
['foodType' => 'fruits', 'itemID' => 2, 'itemName' => 'banana'],
['foodType' => 'veggies', 'itemID' => 3, 'itemName' => 'carrot'],
['foodType' => 'veggies', 'itemID' => 4, 'itemName' => 'broccoli']
];
function array_column_multi ($array, $column) {
$types = array_unique(array_column($array, $column));
$return = [];
foreach ($types as $type) {
foreach ($array as
value) {
if ($type === $value[$column]) {
unset($value[$column]);
$return[$type][] = $value;
unset($array[$key]);
}
}
}
return $return;
}
print_r(array_column_multi($data, 'foodType'));
https://3v4l.org/KQVeN
Result:
Array
(
[fruits] => Array
(
[0] => Array
(
[itemID] => 1
[itemName] => apple
)
[1] => Array
(
[itemID] => 2
[itemName] => banana
)
)
[veggies] => Array
(
[0] => Array
(
[itemID] => 3
[itemName] => carrot
)
[1] => Array
(
[itemID] => 4
[itemName] => broccoli
)
)
)
Oh I just noticed that you're aggregating them by ID. There's not a function for that, you're going to need to iterate over the input with a loop, and populate an output array with the data you want. Eg:
$output = [];
foreach($input_array as $item) {
$output[$item['id']][] = [
'itemID' => $item['itemID'],
'itemName' => $item['itemName']
];
}
You can use array_walk_recursive
$array = [['a', 'b', 'c'], ['x', 'y', 'z']];
$findme = 'y';
$key = null;
array_walk_recursive($array, function($v, $k) use($findme, &$key){
if($v === $findme) $key = $k;
});
echo $key;
Working example : https://3v4l.org/GUqGH
In case if you need both keys use array_walk
array_walk($array, function($v , $k) use (&$keys, $findme){
$found = array_search($findme, $v);
isset($found) ? ($keys = [$k, $found]) : '';
});
print_r($keys);
Working example : https://3v4l.org/U6bhn
You can use foreach to search all the target value,
$search = "y";
$keys = [];
$array = [['a', 'b', 'c'], ['x', 'y', 'z']];
foreach($array as $row_index => $value){
foreach($value as $col_index => $v){
if($search == $v){
$keys[] = array(
"row_index" => $row_index,
"col_index" => $row_index
);
}
}
}
Try the following function.
function my_merge_array( $arr = [] ){
$result = [];
foreach ( $arr as $_arr ) {
$A = $_arr['A'];
foreach ($_arr['B'] as $key => $value) {
$result[$A][$key] = [ $value, $_arr['C'][$key] ];
}
}
return $result;
}
print_r( my_merge_array( $oriArr ) );
I wasn't able to do it entirely with array_column, but this works ... as long as the product and item arrays have the same number of elements.
<?php
$oriArr = array(
2 => array(
'A' => 'Mapping item1',
'B' => array(1 => 'product1', 2 => 'product2', 3 => 'product3'),
'C'=>array(1 => 'item1', 2 => 'item2', 3 => 'item3')
),
3 => array(
'A' => 'Mapping item2',
'B' => array(1 => 'product4', 2 => 'product5', 3 => 'product6'),
'C'=>array(1 => 'item4', 2 => 'item5', 3 => 'item6')
)
);
$keys = array_column($oriArr,'A');
$products = array_column($oriArr,'B','A');
$items = array_column($oriArr,'C','A');
$result = [];
foreach ($keys as $k) {
$result[$k] = [];
if (isset($products[$k])) {
$i = 1;
foreach ($products[$k] as $p) {
$result[$k][] = [$products[$k][$i],$items[$k][$i]];
$i++;
}
}
}
var_dump($keys,$products,$items);
var_dump($result);
Given a bidimensional array $channels:
$channels = array(
array(
'id' => 100,
'name' => 'Direct'
),
array(
'id' => 200,
'name' => 'Dynamic'
)
);
A nice way is using array_map:
$_currentChannels = array_map(function ($value) {
return $value['name'];
}, $channels);
and if you are a potentate (php 5.5+) through array_column:
$_currentChannels = array_column($channels, 'name');
Both results in:
Array
(
[0] => Direct
[1] => Dynamic
)
Star guests: array_map (php4+) and array_column (php5.5+)
// array array_map ( callable $callback , array $array1 [, array $... ] )
// array array_column ( array $array , mixed $column_key [, mixed $index_key = null ] )
Is there a built in way for me to simply extract each of these rows from the array without having to loop in.
Not yet. There will be a function soon named array_column(). However the complexity will be the same, it's just a bit more optimized because it's implemented in C and inside the PHP engine.