PHP Sort multi-dimension array by keys - php

I have a multi dimension array like below:
Array
(
[1200] => Array
(
[B] => Array
(
[4] => Array
(
[Name] => 'Joe']
)
)
[A] => Array
(
[3] => Array
(
[Name] => 'Paul']
)
)
)
[1100] => Array
(
[F] => Array
(
[2] => Array
(
[Name] => 'Sam']
)
)
[D] => Array
(
[1] => Array
(
[Name] => 'Jane']
)
)
)
What I wish to achieve is having the 4 digit number 1100 and 1200 in order ascending, then I need the letters (B A) and (F D) also in order, and then the single digit number under them in order ascending too. I believe I'm looking at a multi dimension array but any help would be appreciated.

The below function might be what you're looking for. It recursively orders arrays by their key.
function ksort_r(&$array) {
foreach ($array as &$value) {
if (is_array($value)) {
ksort_r($value);
}
}
return ksort($array);
}
Example usage
function ksort_r(&$array) {
foreach ($array as &$value) {
if (is_array($value)) {
ksort_r($value);
}
}
return ksort($array);
}
$data = [
1200 => [
'B' => [
4 => [
'Name' => 'Joe'
]
],
'A' => [
3 => [
'Name' => 'Paul'
]
]
],
1100 => [
'F' => [
2 => [
'Name' => 'Sam'
]
],
'D' => [
1 => [
'Name' => 'Jane'
]
]
]
];
ksort_r($data);
print_r($data);
The above will output...
Array
(
[1100] => Array
(
[D] => Array
(
[1] => Array
(
[Name] => Jane
)
)
[F] => Array
(
[2] => Array
(
[Name] => Sam
)
)
)
[1200] => Array
(
[A] => Array
(
[3] => Array
(
[Name] => Paul
)
)
[B] => Array
(
[4] => Array
(
[Name] => Joe
)
)
)
)

Related

how to Grouping array in php

I would like to know how to combine and overwrite the already existed array value in php.
my current array look like:
Array
(
[1149] => 3
[4108] => 5
)
As shown above values, i need expected result in php as below :
Array
(
[0] => Array
(
[offer_id] => 1149
[quantity] => 3
)
[1] => Array
(
[offer_id] => 4108
[quantity] => 5
)
)
How about:
$result = [];
foreach (
[
1149 => 3,
4108 => 3,
] as $key => $value
) {
$result[] = [
'offer_id' => $key,
'quantity' => $value,
];
}
print_r($result);

Shift array elements one level to the right (down one level) when using foreach

Sorry if there's an obvious answer, but I've been trying for hours to google the solution and tried various ways. Basically I'm trying to shift an array within a multidimensional array one level to the right. The output array is to be fed into a program which only accepts the desired format of multidimensional array.
We have ($result):
Array (
[0] => Array (
[id] => 1
[data] => AAA
)
[1] => Array (
[id] => 2
[data] => BBB
)
[2] => Array (
[id] => 3
[data] => CCC
)
)
We want to insert a new array [test] in between the multidimensional array. Desired result:
Array (
[0] => Array (
[test] => Array (
[id] => 1
[data] => AAA
)
)
[1] => Array (
[test] => Array (
[id] => 2
[data] => BBB
)
)
[2] => Array (
[test] => Array (
[id] => 3
[data] => CCC
)
)
)
What I've got so far:
foreach ($result as $key => $value) {
$newarray[] = array($key => $value)
};
return $newarray;
Unfortunately the above inserts an incremental index instead of [test] where we want it to.
Can anyone help?
You could use a loop and index in the same array using the current key. Then update the value with a new array where using test as the key and $v as the value.
$arrays = [
0 => [
"id" => 1,
"data" => "AAA"
],
1 => [
"id" => 2,
"data" => "BBB"
],
2 => [
"id" => 3,
"data" => "CCC"
],
];
foreach ($arrays as $k => $v) {
$arrays[$k] = ['test' => $v];
};
print_r($arrays);
Result
Array
(
[0] => Array
(
[test] => Array
(
[id] => 1
[data] => AAA
)
)
[1] => Array
(
[test] => Array
(
[id] => 2
[data] => BBB
)
)
[2] => Array
(
[test] => Array
(
[id] => 3
[data] => CCC
)
)
)
Demo
Try this:
foreach ($result as $key => $value) {
$newarray[$key] = ['test' => $value];
};
This would be a working and flexible approach:
<?php
$data = [
[
'id' => 1,
'data' => "AAA"
],
[
'id' => 2,
'data' => "BBB"
],
[
'id' => 3,
'data' => "CCC"
]
];
array_walk($data, function(&$entry) {
$entry = [
'test' => $entry
];
});
print_r($data);
The output obviously is:
Array
(
[0] => Array
(
[test] => Array
(
[id] => 1
[data] => AAA
)
)
[1] => Array
(
[test] => Array
(
[id] => 2
[data] => BBB
)
)
[2] => Array
(
[test] => Array
(
[id] => 3
[data] => CCC
)
)
)
$data = [
[
'id' => 1,
'data' => "AAA"
],
[
'id' => 2,
'data' => "BBB"
],
[
'id' => 3,
'data' => "CCC"
]
];
$newArray = array_map(function ($value) {
return ['test' => $value];
}, $data);
var_dump($newArray);
https://secure.php.net/manual/en/function.array-map.php

change a key of a single element array

I have an array tree from a database, I want to change the key of a child element in this case the second array 'eric'=>array into integer '0'=>array as follow :
0 => Array
('text' => 'paris',
'nodes' => Array
('eric' => Array
( 'text' => 'eric',
'nodes' => Array
(0 => Array
(
'text' => 'so.png',
),
),
),
),
),
there is my code :
while($d = mysqli_fetch_assoc($result)) {
if(!isset($data[$d['country']])) {
$data[$d['country']] = array(
'text' => $d['country'],
'nodes' => array()
);
}
if(!isset($data[$d['country']]['nodes'][$d['name']])) {
$data[$d['country']]['nodes'][$d['name']] = array(
'text' => $d['name'],
'nodes' => array()
);
}
array_push($data[$d['country']]['nodes'][$d['name']]['nodes'], $d['n_doc']);
}
To change all of the child keys to numeric values, you can simply just use array_values()
Live Demo
for($i = 0; $i <= count($data) -1; $i++) { # This loops through each country
$data[$i]['nodes'] = array_map(function($node) { # This preserves the parent text value
return array_values($node); # [0] => Paris, [1] => array(...)
}, $data[$i]['nodes']);
}
Output
[ ... => [ text => Paris, nodes => [ 0 => Paris, 1 => [ ... ] ] ... ] ... ]
can you change your code for this input:
Array
(
[0] => Array
(
[text] => paris
[nodes] => Array
(
[jacque] => Array
(
[text] => jacque
[nodes] => Array
(
[0] => 32.png
)
)
[anis] => Array
(
[text] => anis
[nodes] => Array
(
[0] => 5384a97ee9d6b (2).pd
)
)
)
)
[1] => Array
(
[text] => london
[nodes] => Array
(
[dodo] => Array
(
[text] => dodo
[nodes] => Array
(
[0] => 148782.svg
[1] => 333.png
)
)
[sd] => Array
(
[text] => sd
[nodes] => Array
(
[0] => 1014-favicon.ico
)
)
)
)
)

compare values from multidimensional array and add key to array

Array
(
[681074CRPAK4] => Array
(
[0] => 681074
[1] => 681074CRPAK4
[2] => 5602385431605
)
[681520XXXP6L] => Array
(
[0] => 681520
[1] => 681520XXXP6L
[2] => 5602385667394
)
[681530XXXP6V] => Array
(
[0] => 681530
[1] => 681530XXXP6V
[2] => 5602385667417
)
[681530XXXP6W] => Array
(
[0] => 681530
[1] => 681530XXXP6W
[2] => 5602385667424
)
[681530XXXP6X] => Array
(
[0] => 681530
[1] => 681530XXXP6X
[2] => 5602385667400
)
)
I want to compare the value of key[0] of each array.
If they are the same then I would like to add a new key[3] to each array with an id.
This is an array of variable products if the product has the same key[0] then its the same product with different variations.
If the key[0] is different from the previous then add id+1, in this example, I would like to end up with:
Array
(
[681074CRPAK4] => Array
(
[0] => 681074
[1] => 681074CRPAK4
[2] => 5602385431605
[3] => 1
)
[681520XXXP6L] => Array
(
[0] => 681520
[1] => 681520XXXP6L
[2] => 5602385667394
[3] => 2
)
[681530XXXP6V] => Array
(
[0] => 681530
[1] => 681530XXXP6V
[2] => 5602385667417
[3] => 3
)
[681530XXXP6W] => Array
(
[0] => 681530
[1] => 681530XXXP6W
[2] => 5602385667424
[3] => 3
)
[681530XXXP6X] => Array
(
[0] => 681530
[1] => 681530XXXP6X
[2] => 5602385667400
[3] => 3
)
)
can you guys help me with this?
I tried this:
but does not work
foreach ($new as $current_key => $current_array) {
foreach ($new as $search_key => $search_array) {
$ref1 = $current_array[0];
$ref2 = $search_array[0];
if (($search_key != $current_key) and ($ref1 == $ref2)) {
$current_array[3] = $p_id_product;
}
else{
$current_array[3] = $p_id_product++;
}
}
}
Assuming you have already sorted the array by the initial index, so at least they are grouped:
<?php
$data =
[
[
'foo',
'spam',
'bar',
],
[
'foo',
'eggs',
],
[
'bar',
'ham'
],
];
$output = [];
$counter = 0;
$last = null;
foreach($data as $k => $v) {
if($last !== $v[0])
$counter++;
$v[3] = $counter;
$output[$k] = $v;
$last = $v[0];
}
var_export($output);
Output:
array (
0 =>
array (
0 => 'foo',
1 => 'spam',
2 => 'bar',
3 => 1,
),
1 =>
array (
0 => 'foo',
1 => 'eggs',
3 => 1,
),
2 =>
array (
0 => 'bar',
1 => 'ham',
3 => 2,
),
)

make nested array on the basis of specific array value in php

Array
(
[0] => Array
(
[user_id] => 40718
[name] => abc1
)
[1] => Array
(
[user_id] => 40718
[name] => abc2
)
[2] => Array
(
[user_id] => 40719
[name] => abc3
)
)
my array is like having user_id as you can see above i want to convert it into nested array on the basis of specific value from array as user_id like mention below
Array
(
[40718] => Array
(
[0]=>array(
[name] => abc1
)
[1]=>array(
[name] => abc2
)
)
[40719] => Array
(
[0] => (
[name] => abc3
)
)
)
Though I haven't tested it, check if it can help you to get your result :
<?php
$arrTest = [
[
'user_id' => 40718,
'name' => 'abc1'
],
[
'user_id' => 40718,
'name' => 'abc2'
],
[
'user_id' => 40719,
'name' => 'abc3'
]
];
$resultArr = [];
foreach ($arrTest as $val) {
$resultArr[$val['user_id']][]['name'] = $val['name'];
}
echo '<pre>'; print_r($resultArr); exit;
?>

Categories