I need to sort an array that can look like this:
$array[4][0] = array('id' => 1, 'value' => 2);
$array[3][2] = array('id' => 0, 'value' => 3);
$array[4][1] = array('id' => 1, 'value' => 0);
$array[1][3] = array('id' => 2, 'value' => 1);
$array[1][1] = array('id' => 3, 'value' => 0);
$array[3][0] = array('id' => 2, 'value' => 1);
$array[3][1] = array('id' => 1, 'value' => 0);
$array[1][2] = array('id' => 3, 'value' => 2);
$array[1][0] = array('id' => 2, 'value' => 1);
$array[2][1] = array('id' => 0, 'value' => 2);
$array[2][4] = array('id' => 3, 'value' => 1);
But needs to be sorted and returned as this:
$array[1][0] = array('id' => 2, 'value' => 1);
$array[1][1] = array('id' => 3, 'value' => 0);
$array[1][2] = array('id' => 3, 'value' => 2);
$array[1][3] = array('id' => 2, 'value' => 1);
$array[2][1] = array('id' => 0, 'value' => 2);
$array[2][4] = array('id' => 3, 'value' => 1);
$array[3][0] = array('id' => 2, 'value' => 1);
$array[3][1] = array('id' => 1, 'value' => 0);
$array[3][2] = array('id' => 0, 'value' => 3);
$array[4][0] = array('id' => 1, 'value' => 2);
$array[4][1] = array('id' => 1, 'value' => 0);
Can anyone help me? It needs to sort both indexes of the array from lowest to highest index value. Sounds simple enough, but I'm having the hardest time trying to figure this out, while still keeping the values intact.
Please help someone...
A quick'n'dirty solution might look something like:
// Sort the outer array
ksort($array);
// Sort each inner array
foreach($array as &$innerArray)
{
ksort($innerArray);
}
You want to sort it by key then, and not by values: http://se.php.net/manual/en/function.ksort.php or http://se.php.net/manual/en/function.uksort.php
Edit,
Example;
function sorter(array &$multidimensional) {
foreach ($multidimensional as &$current) {
if (is_array($current))
sorter($current);
}
ksort($multidimensional);
}
Something like this should do it:
function ksort_recursive(&$arr) {
foreach($arr as $key => &$value) {
if(is_array($value)) {
ksort_recursive($value);
}
} unset($value);
ksort($arr);
}
ksort_recursive($array);
Related
I have 2 associative arrays.
$a1 = array(
'blue' =>
array('id' => 2365,
'level' => 1,
'name' => 'blue'),
'black' =>
array('id' => 478,
'level' => 1,
'name' => 'black'),
'green' =>
array('id' => 698,
'level' => 1,
'name' => 'green'),
'red' =>
array('id' => 169,
'level' => 1,
'name' => 'red')
$a2= array(
'green' =>
array('id' => 452,
'level' => 1,
'name' => 'green'),
'black' =>
array('id' => 124,
'level' => 1,
'name' => 'black'),
'red' =>
array('id' => 124,
'level' => 1,
'name' => 'red'),
'blue' =>
array('id' => 145,
'level' => 1,
'name' => 'blue')
);
I want to sort my second array in the first array key order. That means I want to sorry my array of blue, black, green, red.
My desired result like:
$a2= array(
'blue' =>
array('id' => 145,
'level' => 1,
'name' => 'blue'),
'black' =>
array('id' => 124,
'level' => 1,
'name' => 'black'),
'green' =>
array('id' => 452,
'level' => 1,
'name' => 'green'),
'red' =>
array('id' => 124,
'level' => 1,
'name' => 'red'),
);
For this I am using the code :
uasort($a2, function($a, $b) use ($a1) { return array_search($a['name'], $a1) - array_search($b['name'], $a1); }
But I didn't get my desired result.
Also I tried with this:
uksort($a2, function($key1, $key2) use ($a1) {
return (array_search($key1, $a1) - array_search($key2, $a1));
});
Another method: But again, I didn't get my desired result.
$price = array();
foreach ($a2 as $key => $row) {
$price[$key] = $row['name'];
}
array_multisort($price, SORT_DESC, $a1);
I just use the following concept:
$arr2ordered = array() ;
foreach (array_keys($a1) as $key) {
$arr2ordered[$key] = $a2[$key] ;
}
After that, I got my desired result.
I have a multidimensional array like this.
arr[4]=array('name' => 'ab', 'aid' => 1 'qty' => 10);
arr[5]=array('name' => 'ac', 'aid' => 3 'qty' => 15);
arr[3]=array('name' => 'ad', 'aid' => 2 'qty' => 100);
arr[2]=array('name' => 'ae', 'aid' => 2 'qty' => 150);
and I need to sort them by the 'aid' but don't change the main index like this.
arr[4]=array('name' => 'ab', 'aid' => 1 'qty' => 10);
arr[2]=array('name' => 'ae', 'aid' => 2 'qty' => 150);
arr[3]=array('name' => 'ad', 'aid' => 2 'qty' => 100);
arr[5]=array('name' => 'ac', 'aid' => 3 'qty' => 15);
is that possible? what should I do? thanks.
From PHP manual
<?php
$data[] = array('volume' => 67, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 1);
$data[] = array('volume' => 85, 'edition' => 6);
$data[] = array('volume' => 98, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 6);
$data[] = array('volume' => 67, 'edition' => 7);
?>
In this example, we will order by volume descending, edition ascending.
We have an array of rows, but array_multisort() requires an array of columns, so we use the below code to obtain the columns, then perform the sorting.
<?php
// Obtain a list of columns
foreach ($data as $key => $row) {
$volume[$key] = $row['volume'];
$edition[$key] = $row['edition'];
}
// Sort the data with volume descending, edition ascending
// Add $data as the last parameter, to sort by the common key
array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);
?>
http://php.net/manual/en/function.array-multisort.php
Edit
For your need you requested:
$arr[4]=array('name' => 'ab', 'aid' => 1, 'qty' => 10);
$arr[5]=array('name' => 'ac', 'aid' => 3, 'qty' => 15);
$arr[3]=array('name' => 'ad', 'aid' => 2, 'qty' => 100);
$arr[2]=array('name' => 'ae', 'aid' => 2, 'qty' => 150);
$keyPositions = array();
foreach ($arr as $key => $value) {
$keyPositions[] = $key;
}
foreach ($arr as $key => $row) {
$aid[$key] = $row['aid'];
}
array_multisort($aid, SORT_ASC, $arr);
$sortedArray = array();
foreach ($arr as $key => $value) {
$sortedArray[$keyPositions[$key]] = $value;
}
echo '<pre>';
print_r($sortedArray);
echo '</pre>';
?>
How to sum array the same value of element ?
$arr = [
['id' => 1, 'qty' => 100, 'name' => 'a'],
['id' => 1, 'qty' => 100, 'name' => 'a'],
['id' => 2, 'qty' => 100, 'name' => 'b']
];
become to :
$arr = [
['id' => 1, 'qty' => 200, 'name' => 'a'],
['id' => 2, 'qty' => 100, 'name' => 'b']
];
i was try but return
[ 1=>['qty'=>200], 2=>['qty'=>100] ]
i was try but return
for($i=0; $i<count($cok);$i++){
$item_id = $cok[$i]['id'];
$quantity = $cok[$i]['quantity'];
if (isset($new_items[$item_id])) {
$new_items[$item_id] = ['quantity' => $new_items[$item_id]['quantity'] + $quantity];
} else {
$new_items[$item_id] = ['quantity' => $quantity];
}
}
Simple as pie (:
<?php
$arr = array(
array('id' => 1, 'qty' => 100, 'name' => 'a'),
array('id' => 1, 'qty' => 100, 'name' => 'a'),
array('id' => 2, 'qty' => 100, 'name' => 'b')
);
$new_arr = array();
foreach($arr AS $item) {
if(isset($new_arr[$item['id']])) {
$new_arr[$item['id']]['qty'] += $item['qty'];
continue;
}
$new_arr[$item['id']] = $item;
}
$arr = array_values($new_arr);
var_dump($arr);
Dive into my viper
And inplace snippet, returns exactly you need:
<?
$arr = array(
array('id' => 1, 'qty' => 100, 'name' => 'a'),
array('id' => 1, 'qty' => 100, 'name' => 'a'),
array('id' => 2, 'qty' => 100, 'name' => 'b')
);
$ids = array();
foreach ($arr as $i => $subarray) {
if (!($remove_from_array = array_key_exists($subarray['id'], $ids))) {
$ids[$subarray['id']] = 0;
}
$ids[$subarray['id']] += $subarray['qty'];
if ($remove_from_array) {
unset($arr[$i]);
}
}
foreach ($arr as &$subarray) {
$subarray['qty'] = $ids[$subarray['id']];
}
print_r($arr);
I have an array with the following format:
$array = Array(
Array('id' => 77, 'title' => 'title'),
Array('id' => 43, 'title' => 'title2'),
Array('id' => null, 'title' => 'title3'),
Array('id' => null, 'title' => null),
);
This array is populated dynamically, this is just an example. Also, i have a second array with the format:
$searchingArray = Array('43', '5');
The main idea is to search if values from $searchingArray are in $array and if not exists then added. My function is:
function addId($id, $ignoreIfFound=false) {
foreach ($array as $values) {
if ($values['id'] and $values['id'] == $id) {
if (!$ignoreIfFound) {
$array[] = Array('id' => $id, 'title' => 'test5');
break;
}
else{
// do nothing
}
}else{
$array[] = Array('id' => $id, 'title' => 'test5');
break;
}
}
}
foreach ($searchingArray as $id) {
$this->addId($id, true);
}
For given example the result should be:
$array = Array(
Array('id' => 77, 'title' => 'title'),
Array('id' => 43, 'title' => 'title2'),
Array('id' => null, 'title' => 'title3'),
Array('id' => null, 'title' => null),
Array('id' => 5, 'title' => 'test5'),
);
Can you tell me what it is wrong with my code?
This should work for you:
First extract the id column out of your array with array_column(). After this simply loop through your search array and check with in_array() if they id already exists or not. If not simply add it to the array.
$ids = array_column($array, "id");
foreach($searchingArray as $search) {
if(!in_array($search, $ids)) {
$array[] = ["id" => $search, "title" => "title" . $search];
}
}
Having a problem here that maybe someone can help me with. Hours of searching couldn't find the solution so I'm at my wit's end.
I have an array,
$array = array(
array('id' => 1, 'parent_id' => 0, 'name' => 'Main'),
array('id' => 2, 'parent_id' => 0, 'name' => 'Something'),
array('id' => 3, 'parent_id' => 1, 'name' => 'Child of Main'),
array('id' => 4, 'parent_id' => 3, 'name' => 'Child of Child of Main'),
...
);
I want a function that will give me all of the ids of the parents for a given node, i.e.
$ids = getIDs(4);
would return an array of {3, 1}
Any suggestions? Thank you in advance.
I've made something similar to RiaD (but on my own). It works well with the structure you already have.
<?php
$array = array(
array('id' => 1, 'parent_id' => 0, 'name' => 'Main'),
array('id' => 2, 'parent_id' => 0, 'name' => 'Something'),
array('id' => 3, 'parent_id' => 1, 'name' => 'Child of Main'),
array('id' => 4, 'parent_id' => 3, 'name' => 'Child of Child of Main'),
);
$parents = array();
function getIDs($id){
global $array, $parents;
$tmp = $array[$id-1]['parent_id'];
if($tmp){
array_push($parents, $tmp);
getIDs($tmp);
}
}
getIDs(4);
var_dump($parents);
?>
Is it possible to make structure like this?
$array = array(
1=>array('id' => 1, 'parent_id' => 0, 'name' => 'Main'),
2=>array('id' => 2, 'parent_id' => 0, 'name' => 'Something'),
3=>array('id' => 3, 'parent_id' => 1, 'name' => 'Child of Main'),
4=>array('id' => 4, 'parent_id' => 3, 'name' => 'Child of Child of Main'),
...
);
with or without 'id' inside array
This one should works with this structure
function getIds($array,$x){
if(!$array[$x]['parent_id'])
return array();
else
return array_merge(array($array[$x]['parent_id']),getIds($array,$array[$x]['parent_id']));
}
you can try foreach loop or use array_count_values