Assume I have two arrays $array1 :
array (size=3)
0 =>
array (size=5)
'id' => int 16
'project_id' => string '37' (length=2)
'description' => string 'Guitar' (length=6)
'qty' => int 87
'uom' => string 'Units' (length=5)
1 =>
array (size=5)
'id' => int 17
'project_id' => string '37' (length=2)
'description' => string 'Drums' (length=5)
'qty' => int 889
'uom' => string 'Drum' (length=4)
2 =>
array (size=5)
'id' => int 13
'project_id' => string '37' (length=2)
'description' => string 'Bump' (length=4)
'qty' => int 76
'uom' => string 'Units' (length=5)
and $array2 :
array (size=2)
0 =>
array (size=3)
'id' => int 17
'qty' => int 800
1 =>
array (size=3)
'id' => int 16
'qty' => int 87
I need some help:
merge above arrays by id and do subtraction of qty values
check if qty values less than or equal to 0 do not include in $result.
so the final $result would be:
array (size=2)
0 =>
array (size=5)
'id' => int 17
'project_id' => string '37' (length=2)
'description' => string 'Drums' (length=5)
'qty' => int 89
'uom' => string 'Drum' (length=4)
1 =>
array (size=5)
'id' => int 13
'project_id' => string '37' (length=2)
'description' => string 'Bump' (length=4)
'qty' => int 76
'uom' => string 'Units' (length=5)
Any help would be appreciated, thanks.
Try this:
foreach($array1 as $a) {
$match = false;
foreach($array2 as $b) {
if ($a['id'] == $b['id']) {
$match = true;
if (($a['qty'] - $b['qty']) > 0) {
$a['qty'] -= $b['qty'];
$result[] = $a;
break;
}
}
}
if(!$match) $result[] = $a;
}
Related
I have the following arrays:
array (size=3)
0 =>
array (size=3)
'id' => int 18
'class' => string 'VIP' (length=3)
'fee' => float 20
1 =>
array (size=3)
'id' => int 19
'class' => string 'VVIP' (length=4)
'fee' => float 50
2 =>
array (size=3)
'id' => int 20
'class' => string 'STANDARD' (length=8)
'fee' => float 5
array (size=3)
0 =>
array (size=3)
'id' => int 19
'class' => string 'VVIP' (length=4)
'fee' => int 50
1 =>
array (size=3)
'id' => int 18
'class' => string 'VIP' (length=3)
'fee' => int 20
2 =>
array (size=3)
'id' => int 20
'class' => string 'STANDARD' (length=8)
'fee' => int 5
Now i am trying to sort them both using array_sort in ascending order using the id.:
$array_1 = array_sort($array_1, 'id', SORT_ASC);
$array_2 = array_sort($array_2, 'id', SORT_ASC);
However i keep getting the following error:
Argument 2 passed to array_sort() must be callable, string given
The second parameter of array_sort should be a closure, not a string:
$array_1 = array_sort( $array_1, function($value){
return $value['id'];
});
I have an array that contains products and their categoryID, some of products have the same category. the output of the array is below
array (size=5)
'categoryID' =>
array (size=3)
0 => string '5' (length=1)
1 => string '2' (length=1)
2 => string '2' (length=1)
'name' =>
array (size=3)
0 => string 'HP Player' (length=9)
1 => string 'Android App' (length=11)
2 => string 'TV' (length=2)
'price' =>
array (size=3)
0 => string '600' (length=3)
1 => string '111' (length=3)
2 => string '1' (length=1)
'qty' =>
array (size=3)
0 => string '22' (length=2)
1 => string '22' (length=2)
2 => string '222' (length=3)
'exprice' =>
array (size=3)
0 => string '13200' (length=5)
1 => string '2442' (length=4)
2 => string '222' (length=3)
I want to combine the products that have same categoryID, the output should be like below:
array (size=2)
=> 0 array
'categoryID' =>
array
0 => string '5' (length=1)
'name' =>
array
0 => string 'HP Player' (length=9)
'price' =>
array
0 => string '600' (length=3)
'qty' =>
array
0 => string '22' (length=2)
'exprice' =>
array
0 => string '13200' (length=5)
=> 1 array
'categoryID' =>
array
0 => string '2' (length=1)
'name' =>
array
0 => string '2' (length=1)
1 => string '2' (length=1)
'price' =>
array
0 => string '111' (length=3)
1 => string '1' (length=1)
'qty' =>
array
0 => string '22' (length=2)
1 => string '222' (length=3)
'exprice' =>
array
0 => string '2442' (length=4)
1 => string '222' (length=3)
how could I do that? Thanks
$arr = array (
'categoryID' =>
array (
0 => '5',
1 => '2',
2 => '2'
),
'name' =>
array (
0 => 'HP Player',
1 => 'Android App',
2 => 'TV'
),
'price' =>
array (
0 => '600',
1 => '111',
2 => '1'
),
'qty' =>
array (
0 => '22',
1 => '22',
2 => '222'
),
'exprice' =>
array (
0 => '13200',
1 => '2442',
2 => '222'
)
);
$cat_count = count(array_keys($arr['categoryID']));
$new_arr = array();
for($i=0;$i<$cat_count;$i++){
for($j=0;$j<$i;$j++){
if($arr['categoryID'][$i] == $arr['categoryID'][$j]){
$arr_key = array_search($arr['categoryID'][$i], $arr['categoryID']);
$cat_exists = 1;
}
}
if($cat_exists){
$new_arr[$arr_key]['name'][] = $arr['name'][$i];
$new_arr[$arr_key]['price'][] = $arr['price'][$i];
$new_arr[$arr_key]['qty'][] = $arr['qty'][$i];
$new_arr[$arr_key]['exprice'][] = $arr['exprice'][$i];
}else{
$new_arr[$i]['categoryID'][] = $arr['categoryID'][$i];
$new_arr[$i]['name'][] = $arr['name'][$i];
$new_arr[$i]['price'][] = $arr['price'][$i];
$new_arr[$i]['qty'][] = $arr['qty'][$i];
$new_arr[$i]['exprice'][] = $arr['exprice'][$i];
}
}
P.S :: $arr is input array & $new_arr is output(result) array!
I have this multidimensional array
I am wondered how can i sort this array again so i can use it in for loop.
array (size=3)
0 =>
array (size=1)
0 =>
array (size=7)
'username' => string 'wajdi' (length=5)
'userimage' => string 'file_3898.jpg' (length=13)
'date' => int 1373721708
'postid' => string '118' (length=3)
'type' => string 'comment' (length=7)
'comment' => string 'a' (length=1)
'notify' => string '0' (length=1)
2 =>
array (size=1)
2 =>
array (size=7)
'username' => string 'wajdi' (length=5)
'userimage' => string 'file_3898.jpg' (length=13)
'date' => int 1373721711
'postid' => string '118' (length=3)
'type' => string 'comment' (length=7)
'comment' => string 'c' (length=1)
'notify' => string '0' (length=1)
3 =>
array (size=1)
3 =>
array (size=7)
'username' => string 'wajdi' (length=5)
'userimage' => string 'file_3898.jpg' (length=13)
'date' => int 1373721712
'postid' => string '118' (length=3)
'type' => string 'comment' (length=7)
'comment' => string 'd' (length=1)
'notify' => string '0' (length=1)
How can I reindex this array to become
array (size=3)
0 =>
array (size=1)
0 =>
array (size=7)
'username' => string 'wajdi' (length=5)
'userimage' => string 'file_3898.jpg' (length=13)
'date' => int 1373721708
'postid' => string '118' (length=3)
'type' => string 'comment' (length=7)
'comment' => string 'a' (length=1)
'notify' => string '0' (length=1)
1 =>
array (size=1)
1 =>
array (size=7)
'username' => string 'wajdi' (length=5)
'userimage' => string 'file_3898.jpg' (length=13)
'date' => int 1373721711
'postid' => string '118' (length=3)
'type' => string 'comment' (length=7)
'comment' => string 'c' (length=1)
'notify' => string '0' (length=1)
2 =>
array (size=1)
2 =>
array (size=7)
'username' => string 'wajdi' (length=5)
'userimage' => string 'file_3898.jpg' (length=13)
'date' => int 1373721712
'postid' => string '118' (length=3)
'type' => string 'comment' (length=7)
'comment' => string 'd' (length=1)
'notify' => string '0' (length=1)
I tried array_shift and array_chunk but nothing works !!!
Please help, thank you all :)
Use array_multisort to sort multi-dimensional arrays or to sort an array using multiple keys.
I think this should do it but it's be a lot cleaner if you didn't have the extra level off the array.
$new_array = array();
$index = 0;
foreach($array as $i1 => $a1){
foreach($a1 as $i2 => $a2){
$new_array[$index][$index] = $a2;
}
$index++;
}
You can use 'array_values' for re-indexing which start index from 0. As per your requirement inner array are not starting from 0 but are same as parent array index. You have to use foreach for that. To index the way you want can be done like this:
$info = array(
0 => array (
0 => array (
'username' => 'wajdi',
'userimage' => 'file_3898.jpg',
'date' => 1373721708,
'postid' => '118',
'type' => 'comment',
'comment' => 'a',
'notify' => '0'
)
),
2 => array (
2 => array (
'username' => 'wajdi',
'userimage' => 'file_3898.jpg',
'date' => 1373721708,
'postid' => '118',
'type' => 'comment',
'comment' => 'a',
'notify' => '0'
)
),
3 => array (
3 => array (
'username' => 'wajdi',
'userimage' => 'file_3898.jpg',
'date' => 1373721708,
'postid' => '118',
'type' => 'comment',
'comment' => 'a',
'notify' => '0'
)
)
);
var_dump($info); // original index
$info = array_values($info);
foreach ($info as $key => $value) {
$temp = array();
foreach($value as $k => $v) {
$temp[$key] = $v;
}
$info[$key] = $temp;
}
var_dump($info); // re-index
I have this array
array
0 =>
array
'prodid' => string '2' (length=1)
'qty' => int 2
'price' => string '100.00' (length=6)
'sid' => string '13' (length=2)
2 =>
array
'prodid' => string '3' (length=1)
'qty' => int 1
'price' => string '380.00' (length=6)
'sid' => string '13' (length=2)
1 =>
array
'prodid' => string '8' (length=1)
'qty' => int 1
'price' => string '300.00' (length=6)
'sid' => string '24' (length=2)
How can i split it in to a multi dimentional array where the values 'sid' match like so.
array
0 =>
array
0 =>
array
'prodid' => string '2' (length=1)
'qty' => int 2
'price' => string '100.00' (length=6)
'sid' => string '13' (length=2)
1 =>
array
'prodid' => string '2' (length=1)
'qty' => int 2
'price' => string '100.00' (length=6)
'sid' => string '13' (length=2)
1 =>
array
0 =>
array
'prodid' => string '7' (length=1)
'qty' => int 1
'price' => string '200.00' (length=6)
'sid' => string '15' (length=2)
You pick the value you want to group with as key for a new array and push the member to it
$new = array();
foreach($array as $member)
{
$key = $member['group-value'];
$new[$key][] = $member;
}
You then extract the bare values from the new array by using array_values:
$new = array_values($new);
And that's it. If you want to spare the last part, you can also create the mapping of keys and it's numerical index on your own:
$new = array();
$keys = array();
foreach($array as $member)
{
$key = $member['group-value'];
isset($keys[$key]) || $keys[$key] = count($keys);
$new[$keys[$key]][] = $member;
}
For PHP5.3+
$result = array_reduce (
$array,
function ($item, $result) {
if (!isset($result[$item['sid']])) $result[$item['sid']] = array();
$result[$item['sid']][] = $item;
return $result;
},
array()
)
// $ouput array will be indexed by the 'sid' value
$output = array();
// Iterate over the main array and create a new subarray if
// it doesn't already exist, or add to it if it does.
foreach($input as $subarr) {
if (!isset($output[$subarr['sid']]) {
// New array indexed by sid
$output[$subarr['sid']] = array();
}
// Append the whole array
$output[$subarr['sid']][] = $subarr;
}
Note: this produces slightly different output than you described, in that it uses the sid as array keys rather than just indexing them from 0:
array
// Note key difference...
// sid == 13
13 =>
array
0 =>
array
'prodid' => string '2' (length=1)
'qty' => int 2
'price' => string '100.00' (length=6)
'sid' => string '13' (length=2)
1 =>
array
'prodid' => string '2' (length=1)
'qty' => int 2
'price' => string '100.00' (length=6)
'sid' => string '13' (length=2)
// sid == 15
15 =>
array
0 =>
array
'prodid' => string '7' (length=1)
'qty' => int 1
'price' => string '200.00' (length=6)
'sid' => string '15' (length=2)
I have two arrays:
1. first array:
array
0 =>
array
'id' => int 1
'section_id' => int 2
'class_id' => int 25
'student_id' => int 1
'unixdate' => int 1322553600
'date' => string '2011-11-29' (length=10)
'attendance' => string 'present' (length=7)
1 =>
array
'id' => int 2
'section_id' => int 2
'class_id' => int 25
'student_id' => int 2
'unixdate' => int 1322553600
'date' => string '2011-11-29' (length=10)
'attendance' => string 'absent' (length=6)
2. second array:
array
0 =>
array
'section_id' => int 2
'class_id' => int 25
'student_id' => int 1
'unixdate' => int 1322553600
'date' => string '2011-11-29' (length=10)
'attendance' => string 'absent' (length=7)
1 =>
array
'section_id' => int 2
'class_id' => int 25
'student_id' => int 3
'unixdate' => int 1322553600
'date' => string '2011-11-29' (length=10)
'attendance' => string 'absent' (length=6)
ON these array if values of two keys 'student_id' and 'unixdate' matches I want new array with the id from array one and other from array 2.
OR
replace the 'attendance' value to first array if 'student_id' and 'unixdate' of both array are same.
EDIT
my result should be like this:
array
0 =>
array
'id' => int 1
'section_id' => int 2
'class_id' => int 25
'student_id' => int 1
'unixdate' => int 1322553600
'date' => string '2011-11-29' (length=10)
'attendance' => string 'absent' (length=7)
How can i do this?
did you try with 2 foreach?
$result = array();
foreach($array1 as $data1)
{
foreach($array2 as $data2)
{
if($data1['student_id'] == $data2['student_id'] and
$data1['unixdate'] == $data2['unixdate'])
{
$tmp = array("id" => $data1['id']);
$tmp = array_merge($tmp, $data2);
$result[] = $tmp;
break;
}
}
}