array_map triple dimensional array [duplicate] - php

This question already has answers here:
How do you reindex an array in PHP but with indexes starting from 1?
(12 answers)
How to re-index all subarray elements of a multidimensional array?
(6 answers)
Closed 10 months ago.
How do I run a array_map on a triple dimensional array? Where I want to "clear" the innermost array?
It looks like this:
Array
(
[1] => Array
(
[1] => Array
(
[cat] => Hello!
[url] => hello
)
[5] => Array
(
[cat] => Good job!
[url] => good-job
)
[2] => Array
(
[2] => Array
(
[cat] => How are you?
[url] => how-are-you
)
[6] => Array
(
[cat] => Running shoes
[url] => running-shoes
)
)
)
I want to make it look like this:
Array
(
[1] => Array
(
[1] => Array
(
[cat] => Hello!
[url] => hello
)
[2] => Array
(
[cat] => Good job!
[url] => good-job
)
[2] => Array
(
[1] => Array
(
[cat] => How are you?
[url] => how-are-you
)
[2] => Array
(
[cat] => Running shoes
[url] => running-shoes
)
)
)
This solution Reset keys of array elements in php? "just" works on tow diemensional arrays, if Im not wrong.

you could write a short function to do it with array_map:
function mappingfunction($array){
$remappedarray = array();
foreach($array as $layer){
$remappedarray[] = array_map('array_values', $array);
}
return $remappedarray;
}
if you want to preserve the keys:
function mappingfunction($array){
$remappedarray = array();
foreach($array as $key => $layer){
$remappedarray[$key] = array_map('array_values', $array);
}
return $remappedarray;
}
Untested, but should point you in the right direction.

Related

How to remove duplicate values from a multi dimensional array for specific key in php [duplicate]

This question already has answers here:
How to remove duplicate values from a multi-dimensional array in PHP
(18 answers)
Closed 2 years ago.
I searched for solutions on here but didn't find one for my use case.
I have a big array which is built like this example:
Array
(
[0] => Array
(
[Template] => page.html5
)
[1] => Array
(
[Template] => page2.html5
)
[2] => Array
(
[Template] => page.html5
)
[3] => Array
(
[Template] => page2.html5
)
[4] => Array
(
[Template] => page.html5
)
[5] => Array
(
[Template] => page2.html5
)
[6] => Array
(
[id] => 27
[table] => tl_custom
[type] => text
[data] => Array
(
[fragment] => example
[previewId] => 1
[isActive] => 1
)
)
)
I would like to remove all duplicate values for the array key "Template", but besides that I want the array to stay the way it is.
So afterwards my Array should look like:
Array
(
[0] => Array
(
[Template] => page.html5
)
[1] => Array
(
[Template] => page2.html5
)
[6] => Array
(
[id] => 27
[table] => tl_custom
[type] => text
[data] => Array
(
[fragment] => example
[previewId] => 1
[isActive] => 1
)
)
)
Is there a way to achieve this without using lots of memory?
Thanks for your answers :)
You could use the following logic, which uses:
array_map() to flatten the array with index keys-values, and serialize() (stringify) the last array element so we can use
array_unique() on the result.
Then, to restore the stringified array, i.e. turn it back into an array, we use unserialize().
<?php
$newArr = array_unique(array_map(function ($el) {
return $el['Template'] ?? serialize($el);
}, $arr));
// restore the last element to array
$last = array_key_last($newArr); // (PHP 7 >= 7.3.0)*
$newArr[$last] = unserialize($newArr[$last]);
*if PHP version <7.3.0 use: end($newArr); $last = key($newArr);
Output:
Array
(
[0] => page.html5
[1] => page2.html5
[6] => Array
(
[id] => 27
[table] => tl_custom
[type] => text
[data] => Array
(
[fragment] => example
[previewId] => 1
[isActive] => 1
)
)
)
working demo
The code below loops the array, marks indexes for removal and then another loop does the removals:
$templates = array(); //This will store the remove plan
for ($index = 0; $index < count($input); $index++) {
if (isset($input[$index]["Template"])) { //Ignore items where there is no template
if (isset($templates[$input[$index]["Template"]])) { //Let's check whether we have already seen this template
$templates[$input[$index]["Template"]] = array(); //From now on we will find duplicates for this dude
} else { //Mark for removal
$templates[$input[$index]["Template"]][]=$index;
}
}
}
//Actual removals
foreach($templates => $index) {
//Removing the actual element:
unset($input[$index]["Template"]);
//Remove the parent as well if it becomes empty
if (!count($input[$index])) unset($input[$index]);
}
The memory need for this algorithm is:
average(element_size) * number_of_elements

Search multidimensional array for value

I wonder if there is better (faster) way to search for value in multidimensional array than looping through every item.
Lets say i have
$id_to_search = '16819976033';
And array which is pretty big
Array
(
[0] => Array
(
[id] => Array
(
[0] => 16771055710
[1] => 16776555710
[2] => 16819976033
)
[o] => 21566
[p] => 12597.66
)
[1] => Array
(
[id] => Array
(
[0] => 14089762
)
[o] => 12606
[p] => 1747.49
)
etc ...
)
I can find it if i loop through each item and than compare them but its very slow because array is big.
You can use by array_search function in PHP:
$key = array_search($id_to_search, array_column($YourArray, 'id'));

Sort multidimensional array by given key PHP [duplicate]

This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 5 years ago.
I have a multidimensional array that needs to be reoreder, the array look like this :
[products] => Array
(
[149] => Array
(
[name] => Ichikami1
[qty] => 2
)
[150] => Array
(
[name] => Ichikami2
[qty] => 4
)
[377] => Array
(
[name] => BCL
[qty] => 2
)
)
inside the child array there is 'qty' index, i want to sort the child array by 'qty' index in descending order, so it will look like this:
[products] => Array
(
[0] => Array
(
[name] => Ichikami2
[qty] => 4
)
[1] => Array
(
[name] => Ichikami1
[qty] => 2
)
[2] => Array
(
[name] => BCL
[qty] => 2
)
)
is there a way to do this?
Simply by using array_multisort(), You can do this like:
$qty = array();
foreach ($products as $key => $row)
{
$qty[$key] = $row['qty'];
}
array_multisort($qty, SORT_DESC, $products);

PHP Rename all the keys in arrays [duplicate]

This question already has answers here:
PHP Change Array Keys
(12 answers)
Closed 8 years ago.
I have an array of arrays, something like this:
Array
(
[0] => Array
(
[0] => DC1F180E-FE57-622C-28AE-8194843B4D84
[1] => First Choice
[2] => 1
)
[1] => Array
(
[0] => EB877F3C-7A3B-98A7-9240-580FB797030A
[1] => Second Choice
[2] => 0
)
[2] => Array
(
[0] => D3C0EA56-73D2-C7E3-8236-EEA2400DFA9C
[1] => Third Choice
[2] => 0
)
)
How do I can rename all the keys in "nested" arrays to get something like this in all "nested" arrays:
...
[1] => Array
(
[id] => EB877F3C-7A3B-98A7-9240-580FB797030A
[name] => Second Choice
[status] => 0
)
...
This way you can change keys in your array:
for ($i=0, $c = count($array); $i<$c; ++$i) {
$array[$i]['id'] = $array[$i][0];
$array[$i]['name'] = $array[$i][1];
$array[$i]['status'] = $array[$i][2];
unset($array[$i][0];
unset($array[$i][1];
unset($array[$i][2];
}
You have to use syntax $array[$key1][$key2] to use multidimensional array.
You could use array_walk and array_combine like this:
$a = array(array('foo','bar'),array('foo','bar'));
print_r($a);
$keys = array('first','second');
$new_array = array();
array_walk($a,function($x) use (&$new_array,$keys) {
$new_array[] = array_combine($keys,$x);
});
print_r($new_array);
array_walk goes through each element of your array, applying a callback function. array_combine combines an array of keys with an array of values to produce a new array.
output:
Array ( [0] => Array ( [0] => foo [1] => bar )
[1] => Array ( [0] => foo [1] => bar ) )
Array ( [0] => Array ( [first] => foo [second] => bar )
[1] => Array ( [first] => foo [second] => bar ) )

Change several ARRAY into new ARRAYS [duplicate]

This question already has answers here:
Combine arrays in PHP [duplicate]
(4 answers)
Closed 10 months ago.
I would like to know how to change the contents from several ARRAYS into new ARRAYS.
I have this 3 vars with a ARRAY each, lets say the first var is $number and it has this array:
Array
(
[0] => 1
[1] => 3
[2] => 9
)
The second var is $item and it has this:
Array
(
[0] => house
[1] => car
[2] => bike
)
And the third is $color and it has this:
Array
(
[0] => red
[1] => white
[2] => black
)
How can I change the contents and create new arrays like this:
Array
(
[0] => 1
[1] => house
[2] => red
)
Array
(
[0] => 3
[1] => car
[2] => white
)
Array
(
[0] => 9
[1] => bike
[2] => black
)
You can use array_map:
<?php
$number = [1,3,9];
$item = ['house','car','bike'];
$color = ['red','white','black'];
$res = array_map(null, $number, $item, $color);
print_r($res);
?>
which will output a single array of arrays that you want:
Array
(
[0] => Array
(
[0] => 1
[1] => house
[2] => red
)
[1] => Array
(
[0] => 3
[1] => car
[2] => white
)
[2] => Array
(
[0] => 9
[1] => bike
[2] => black
)
)
You can make a callback function with array_map() that returns each value together:
$result = array();
function merge_arrays($a,$b,$c){
return array($a,$b,$c);
}
$result = array_map("merge_arrays",$number,$item,$color);
DEMO

Categories