I have found a lot of question and some great answers in order to search into an array with PHP. But everytime, the script answered too perfectly to the quesiton, and not globaly OR all arrays are symetricals.
My Array can look like this:
$data = [
'steve' => [
'id' => [
'#text' => 1,
],
'pseudo' => [
'#text' => 'LOL'
],
],
'albert' => [
'id' => [
'#text' => 2,
],
'pseudo' => [
'#text' => 'KILLER'
],
],
'john' => [
'id' => [
'#text' => 3,
],
'pseudo' => [
'#text' => 'NOOBS'
],
],
];
Which mean that my Array can look beautifully symetrically generated, or completely messy and with random subarray.
My aim is to search inside and found the PSEUDO of AN ID. And unfortunately, I can't change the webservice which give me this result.
I had tried with array_column or array_search, but the only thing I had successfully returned is if it founds something, or not. But can't identify it. And my script was very slow.
Something like :
search($array, 2); //which return KILLER
or more optionable maybe?
My Array can have a lot of IDs (100+). So I'm trying to found something optimize. :/
There's probably a million ways to do this, but ultimately you'll need some recursive approach that's able to filter on the value and the path to that value. For this, the following would be one of those million ways.
It uses the predefined iterators:
CallbackFilterIterator
RecursiveIteratorIterator
RecursiveArrayIterator
in combination with a custom PathAsKeyDecorator iterator.
Demo here
<?php
declare(strict_types=1);
final class PathAsKeyDecorator implements \Iterator
{
private RecursiveIteratorIterator $inner;
public function __construct(RecursiveIteratorIterator $inner)
{
$this->inner = $inner;
}
public function current()
{
return $this->inner->current();
}
public function next(): void
{
$this->inner->next();
}
public function key()
{
$path = [];
for ($i = 0, $depth = $this->inner->getDepth(); $i <= $depth; $i++) {
$path[] = $this->inner->getSubIterator($i)->key();
}
return $path;
}
public function valid(): bool
{
return $this->inner->valid();
}
public function rewind(): void
{
$this->inner->rewind();
}
}
$input = [
'steve' => [
'id' => [
'#text' => 1,
],
],
'albert' => [
'id' => [
'#text' => 2,
],
],
'john' => [
'profil' => [
'id' => [
'#text' => 3,
],
],
],
];
// this is the filter function that should be customized given your requirements
// or create a factory function which produces these types of filter functions
$filter = static function ($current, array $path): bool {
// with help from the PathAsKeyDecorator
// we can decide on the path to the current value
return ['id', '#text'] === array_slice($path, -2)
// and the current value
&& 2 === $current;
};
// configure the iterator
$it = new CallbackFilterIterator(
new PathAsKeyDecorator(new RecursiveIteratorIterator(new RecursiveArrayIterator($input))),
$filter,
);
// traverse the iterator
foreach ($it as $path => $val) {
print_r([
'path' => $path,
'val' => $val
]);
}
An alternative approach could be one with simple functions, like:
Demo here
<?php
declare(strict_types=1);
function iterateWithPath(iterable $input, array $path = []): iterable {
foreach ($input as $key => $value) {
$pathToHere = [...$path, $key];
is_iterable($value)
? yield from iterateWithPath($value, $pathToHere)
: yield $pathToHere => $value;
}
}
function filterIterable(iterable $it, callable $filter): iterable {
foreach ($it as $key => $value) {
if ($filter($value, $key)) {
yield $key => $value;
}
}
}
$input = [
'steve' => [
'id' => [
'#text' => 1,
],
],
'albert' => [
'id' => [
'#text' => 2,
],
],
'john' => [
'profil' => [
'id' => [
'#text' => 3,
],
],
],
];
$it = filterIterable(iterateWithPath($input), static function ($current, array $path): bool {
return ['id', '#text'] === array_slice($path, -2)
&& 2 === $current;
});
foreach ($it as $path => $val) {
print_r([
'path' => $path,
'val' => $val
]);
}
complete edit cause of structure change of the array. the json_encode is used for changing the array to a string. Only does its job when there is in every array slice an id and an pseudo.
$data = [
'steve' => [
'id' => [
'#text' => 1,
],
'pseudo' => [
'#text' => 'LOL'
],
],
'albert' => [
'id' => [
'#text' => 2,
],
'pseudo' => [
'#text' => 'KILLER'
],
],
'john' => [
'id' => [
'#text' => 3,
],
'pseudo' => [
'#text' => 'NOOBS'
],
],
];
$data = json_encode($data, JSON_NUMERIC_CHECK);
preg_match_all('~"id":{"#text":([^{]*)}~i', $data, $ids);
preg_match_all('~"pseudo":{"#text":"([^{]*)"}~i', $data, $pseudos);
$lnCounter = 0;
$laResult = array();
foreach($ids[1] as $lnId) {
$laResult[$lnId] = $pseudos[1][$lnCounter];
$lnCounter++;
}
echo $laResult[2];
results in KILLER
Related
I've a huge problem. Somehow I need to get the variation_id from an array based on a value of a child array:
$array = [
[
'attributes' => [
'attribute_art-der-karte' => 'Rot'
],
'variation_id' => '222'
],
[
'attributes' => [
'attribute_art-der-karte' => 'Green'
],
'variation_id' => '221'
]
];
So in my case I've two things available:
The key attribute_art-der-karte
The value Rot
I found nothing here instead of a sorting. Any idea? Thanks!
Simply loop over the array and return the variation ID when your condition is met (an attribute exists with the given key and the given value):
function findVariationId(array $array, string $attrName, string $attrValue): ?string {
foreach ($array as $entry) {
if (($entry['attributes'][$attrName] ?? null) === $attrValue) {
return $entry['variation_id'];
}
}
return null;
}
Demo
You can also use array filter for this
<?php
$array = [
[
'attributes' => [
'attribute_art-der-karte' => 'Rot'
],
'variation_id' => '222'
],
[
'attributes' => [
'attribute_art-der-karte' => 'Green'
],
'variation_id' => '221'
]
];
function findByAttribute ($arr, $value) {
$result = array_filter($arr, function($elem) use($value){
return $elem['attributes']['attribute_art-der-karte'] == $value;
});
if ($result) {
return array_shift($result)['variation_id'];
}
return '';
}
var_dump(findByAttribute($array, 'Rot')); // gives '222'
var_dump(findByAttribute($array, 'a')); // returns ''
var_dump(findByAttribute($array, 'Green')); // gives 221
An additional option using array_search() and array_column():
<?php
$array = [
['attributes' => ['attribute_art-der-karte' => 'Rot'], 'variation_id' => '222'],
['attributes' => ['attribute_art-der-karte' => 'Green'], 'variation_id' => '221']
];
$key = array_search (
'Rot',
array_column(array_column($array, 'attributes'), 'attribute_art-der-karte')
);
echo "variation_id: ". (($key === false) ? 'Not found' : $array[$key]["variation_id"]);
?>
Output:
variation_id: 222
You can access the element variation_id using foreach
// One way using loops identify the non-array element
foreach($array as $ele){
if(!is_array($ele)){
print_r($ele);
}
}
// Otherway for direct calling
print_r($array['variation_id']);
I've one array
$arr = [
'parent' => [
'CHILD' => [
5,6
],
'child' => [
1,2,3,4
],
'Child' => [
5,6,7,8
],
...
]
];
I want to lower the child keys and combine each child having the same case insensitive keys like
$arr = [
'parent' => [
'child' => [
1,2,3,4,5,6,7,8
],
]
];
I've tried with array_change_key_case which always takes the last element and ignores the others.
An array may have multiple children with the same key (with different case)
Try the code below should work:
<?php
$arr = [
'parent' => [
'CHILD' => [
5,6
],
'child' => [
1,2,3,4
],
]
];
$arNew = [];
foreach ($arr as $sParent => $ar) {
foreach ($ar as $sChild => $ar1) {
$sChild = strtolower($sChild);
if (empty($arNew[$sParent][$sChild])) {
$arNew[$sParent][$sChild] = $ar1;
} else {
$arNew[$sParent][$sChild] = array_merge($arNew[$sParent][$sChild], $ar1);
}
}
}
print_r($arNew);
I'd like to sort the following associative array:
$tree = [
"id" => 245974,
"children" => [
[
"id" => 111
],
[
"id" => 245982,
"children" => [
[
"id" => 246093,
"children" => [
[
"id" => 225892
],
[
"id" => 225893
],
[
"id" => 225902
]
]
]
]
]
]
];
Desired sort order after the "search value" of id => 225902:
[
"id" => 245974,
"children" => [
[
"id" => 245982, // <-- this is moved up
"children" => [
[
"id" => 246093,
"children" => [
[
"id" => 225902 // <-- this is moved up
],
[
"id" => 225892
],
[
"id" => 225893
]
]
]
]
],
[
"id" => 111
]
]
];
What I've tried:
<?php
$category_id = 225902;
function custom_sort(&$a, &$b) {
global $category_id;
if ($a['id'] === $category_id) {
return -1;
}
if ($b['id'] === $category_id) {
return 1;
}
if (array_key_exists('children', $a)) {
if (usort($a['children'], "custom_sort")) {
return -1;
}
}
if (array_key_exists('children', $b)) {
if (usort($b['children'], "custom_sort")) {
return 1;
}
}
return 0;
}
function reorder_tree($tree) {
usort($tree['children'], "custom_sort");
return $tree;
}
echo "<pre>";
var_dump(reorder_tree($tree));
echo "</pre>";
However, that returns:
[
"id" => 245974,
"children" => [
[
"id" => 245982, // <- this is moved up
"children" => [
[
"id" => 246093,
"children" => [
[
"id" => 225892
],
[
"id" => 225893
],
[
"id" => 225902 // <- this is *not* moved up
]
]
]
]
],
[
"id" => 111
],
]
];
How would I be able to also sort the children arrays?
Great attempt and very much on the right track. The problem with recursion in the comparator is that usort will not call the comparator function when the array length is 1, so whether or not you explore the whole tree is at the whim of usort. This will abandon id => 245982's branch of the tree.
The solution is to avoid recursing in the usort's comparator function directly. Rather, use a regular recursive function that calls usort as needed, namely, the current array or a child array contains the target id. I use a separate array to keep track of which elements should be moved forward, but you can break out of the loop and splice/unshift a single element to the front if you prefer.
We can also make $category_id a parameter to the function.
Here's one approach:
function reorder_tree_r(&$children, $target) {
$order = [];
$should_sort = false;
foreach ($children as $i => &$child) {
$order[$i] = false;
if (array_key_exists("children", $child) &&
reorder_tree_r($child["children"], $target) ||
$child["id"] === $target) {
$order[$i] = true;
$should_sort = true;
}
}
if ($should_sort) {
$priority = [];
$non_priority = [];
for ($i = 0; $i < count($children); $i++) {
if ($order[$i]) {
$priority[]= $children[$i];
}
else {
$non_priority[]= $children[$i];
}
}
$children = array_merge($priority, $non_priority);
}
return $should_sort;
}
function reorder_tree($tree, $target) {
if (!$tree || !array_key_exists("children", $tree)) {
return $tree;
}
reorder_tree_r($tree["children"], $target);
return $tree;
}
var_export(reorder_tree($tree, 225902));
Output:
array (
'id' => 245974,
'children' =>
array (
0 =>
array (
'id' => 245982,
'children' =>
array (
0 =>
array (
'id' => 246093,
'children' =>
array (
0 =>
array (
'id' => 225902,
),
1 =>
array (
'id' => 225892,
),
2 =>
array (
'id' => 225893,
),
),
),
),
),
1 =>
array (
'id' => 111,
),
),
I've got this PHP array:
<?php
$cities = [
'amsterdam' => $amsterdam,
'prague' => $prague,
'lisboa' => $lisboa
];
$amsterdam = [
65 => [
'table' => Object
'something' => false,
'data' => [
'foo' => 'boo',
'price' => 100
]
],
173 => [
'table' => Object
'something' => false,
'data' => [
'foo' => 'hoo',
'price' => 2500
]
],
...
];
$prague = [
132 => [
'table' => Object
'something' => false,
'data' => [
'foo' => 'boo',
'price' => 2100
]
],
956 => [
'table' => Object
'something' => false,
'data' => [
'foo' => 'hoo',
'price' => 2500
]
],
...
];
$lisboa = [
175 => [
'table' => Object
'something' => false,
'data' => [
'foo' => 'boo',
'price' => 6500
]
],
64 => [
'table' => Object
'something' => false,
'data' => [
'foo' => 'hoo',
'price' => 20
]
],
...
];
?>
and I need to sort it by the subarray value ['data']['price'] so the output is like this:
<?php
$cheapest_cities [
'lisboa' => $lisboa, // because 64->data->price is 20
'amsterdam' => $amsterdam, // beacuse 65->data->price is 100
'prague' => $prague // bacause 132->data->price is 2100
];
?>
I tried several usort combinations, but the problem is, that i never know what the subarray index will be (65, 173, 132, 956, 175, 64) in my example.
Do you have any idea how to sort it?
The data comes from database:
<?php
$amsterdam = $this->getTable()->where(['package_id' => [1,2,3]])->order('package_id')->fetchPairs('id');
$lisboa = $this->getTable()->where(['package_id' => [4,5]])->order('package_id')->fetchPairs('id');
$prague = $this->getTable()->where(['package_id' => [6]])->order('package_id')->fetchPairs('id');
return [
'amsterdam' => $amsterdam,
'lisboa' => $lisboa,
'prague' => $prague,
];
?>
Thank you
I would start by making a new array, which has the smallest price of every city as value
For this I use an array_map function which reduces the $items to the price with array_reduce
$map_prices = function($n) {
$reduce_smallest_price = function($carry, $item) {
return $item['data']['price'] < $carry
? $item['data']['price']
: $carry;
};
return array_reduce($n, $reduce_smallest_price, INF);
};
$cities_price = array_map($map_prices, $cities);
asort($cities_price);
I use this prices array to sort the original array with uksort
uksort($cities, function($a, $b) {
global $cities_price;
return strnatcmp($cities_price[$a], $cities_price[$b]);
});
Here is a live example on 3v4l: https://3v4l.org/8B9VN
Don't use usort as it will remove your keys. Use uasort.
Just a quick idea:
Inside the callback function of uasort you could search the minimum of your item e.g. via array_reduce.
array_reduce($city, function($carry, $item) {
return $carry === 0 ? $item['data']['price'] : min($carry, $item['data']['price']);
}, 0);
This snippet gets the minimum of a city array. Then it should be easy to compare the values.
Full example:
function getMinimumPrice($cityArray) {
return array_reduce($cityArray, function($carry, $item) {
return $carry === 0 ? $item['data']['price'] : min($carry, $item['data']['price']);
}, 0);
}
uasort($cities, function($city1, $city2) {
$priceCity1 = getMinimumPrice($city1);
$priceCity2 = getMinimumPrice($city2);
return $priceCity1 <=> $priceCity2;
});
I am having a hard time trying recursively sort a multidimensional array on its keys. I tried with usort(), but with no success.
Sample data:
[
'first_level' => [
'dir_3' => [
'subdir_1' => [
'file_2.mp4' => (object) [
'name' => 'file_2.mp4',
],
'file_1.mp4' => (object) [
'name' => 'file_1.mp4',
],
],
],
'dir_1' => [
'subdir_2' => [
'file_6.mp4' => (object) [
'name' => 'file_6.mp4',
],
'file_9.mp4' => (object) [
'name' => 'file_9.mp4',
],
'file_7.mp4' => (object) [
'name' => 'file_7.mp4',
],
],
'subdir_1' => [
'file_8.mp4' => (object) [
'name' => 'file_8.mp4',
],
],
],
],
]
Desired result:
[
'first_level' => [
'dir_1' => [
'subdir_1' => [
'file_8.mp4' => (object) [
'name' => 'file_8.mp4',
],
],
'subdir_2' => [
'file_6.mp4' => (object) [
'name' => 'file_6.mp4',
],
'file_7.mp4' => (object) [
'name' => 'file_7.mp4',
],
'file_9.mp4' => (object) [
'name' => 'file_9.mp4',
],
],
],
'dir_3' => [
'subdir_1' => [
'file_1.mp4' => (object) [
'name' => 'file_1.mp4',
],
'file_2.mp4' => (object) [
'name' => 'file_2.mp4',
],
],
],
],
]
Use a recursive function to call ksort on the current level and all deeper subarrays.
function recur_ksort(&$array) {
foreach ($array as &$value) {
if (is_array($value))
recur_ksort($value);
}
ksort($array);
}
recur_ksort($array);
var_export($array);
Demo: https://3v4l.org/Xede5
You need to use ksort with recursion. Demo
function recursive_ksort(&$array) {
foreach ($array as &$v) {
if (is_array($v)) {
recursive_ksort($v);
}
}
ksort($array);
}
recursive_ksort($array);
var_export($array);
function ksort_recursive(&$array)
{
if (is_array($array)) {
ksort($array);
array_walk($array, 'ksort_recursive');
}
}
It is not necessary within the recursive function to return ksort() -- this would return the unwanted success boolean value from ksort() anyhow.
Note that this function does not throw "Warning: ksort() expects parameter 1 to be array" when given a non-array - this matches my requirements but perhaps not yours. Demo: https://3v4l.org/bogAU
It is fair to assume that you'd like your data ti be sorted "naturally" -- meaning that the number portion of the directory and file names should be sorted numerically instead of as simple strings. Without sorting naturally, dir_10 will be moved in front of dir_2 because when comparing the 5th character of the two strings, 1 is less than 2.
Code: (Demo)
function nat_ksort_r(&$data): void
{
if (is_array($data)) {
ksort($data, SORT_NATURAL);
array_walk($data, __METHOD__);
}
}
nat_ksort_r($array);
var_export($array);
For natural sorting, apply the SORT_NATURAL flag to the ksort() call.
For simpler maintenance of the recursive function, recall the function using the __METHOD__ magic constant. This makes one less place to change nat_ksort_r() if you wish to call the custom function something else.
This recursive function does not return any data; it modifies the original array by reference.
The lowest level contains objects and this data does not get sorted by the function.
The above function can also use a classic loop instead of a functional iterator. (Demo)
function nat_ksort_r(&$data): void
{
if (is_array($data)) {
ksort($data, SORT_NATURAL);
foreach ($data as &$item) {
(__METHOD__)($item);
}
}
}
nat_ksort_r($array);
var_export($array);
You can even write the code in a completely anonymous fashion. Demo
$nat_ksort_r = function(&$data) use (&$nat_ksort_r) {
if (is_array($data)) {
ksort($data, SORT_NATURAL);
foreach ($data as &$item) {
$nat_ksort_r($item);
}
}
};
$nat_ksort_r($array);
var_export($array);