I am trying to subtract parts of one nested array from another, but I'm having difficulty specifying the parts that I want to subtract as both values are numbers.
My arrays are, for example:
Array ( [0] => Array ( [id] => 43 [quantity] => 4 ) )
Array ( [0] => Array ( [id] => 43 [quantity] => 2 ) )
And after the subtraction I want the Result to be:
Array ( [0] => Array ( [id] => 43 [quantity] => 2 ) )
I'm using the following code to perform the subtraction, but I can't stop it from subtracting the id from itself:
foreach(array_keys($arrayA) as $id)
{
foreach(array_keys($arrayA[$id]) as $type)
{
$newArray[$id][$type] = $arrayA[$id][$type] - $arrayB[$id][$type];
}
}
print_r($newArray);
Could someone please tell me how I can just effect the [quantity] part of the array, without changing the [id]? With the code as it is I get:
Array ( [0] => Array ( [id] => 0 [quantity] => 2 ) )
Thanks in advance.
$ar1 = array(0 => array('id' => 43, 'quantity' => 4));
$ar2 = array(0 => array('id' => 43, 'quantity' => 2));
$new_array = array();
foreach($ar1 as $key => $value)
{
$new_array[$key] = array('id' => $value['id'], 'quantity' => ($value['quantity'] - $ar2[$key]['quantity']));
}
Array
(
[0] => Array
(
[id] => 43
[quantity] => 2
)
)
Related
I have an array of arrays, and I want to filter that array by multiple key values, and group the arrays with matching keys if there are any. Example array:
Array
(
[0] => Array
(
[id] => 1
[value] => 11
[quantity] => 14
)
[1] => Array
(
[id] => 2
[value] => 11
[quantity] => 14
)
[2] => Array
(
[id] => 3
[value] => 22
[quantity] => 14
)
[3] => Array
(
[id] => 4
[value] => 22
[quantity] => 14
)
[4] => Array
(
[id] => 5
[value] => 23
[quantity] => 15
)
)
and let's say I want the arrays with matching value and quantity to be grouped in a new array
The desired output would be something like this:
Array
(
[11] => Array
(
[0] => Array
(
[id] => 1
[value] => 11
[quantity] => 14
)
[1] => Array
(
[id] => 2
[value] => 11
[quantity] => 14
)
)
[22] => Array
(
[0] => Array
(
[id] => 3
[value] => 22
[quantity] => 14
)
[1] => Array
(
[id] => 4
[value] => 22
[quantity] => 14
)
)
[23] => Array
(
[0] => Array
(
[id] => 5
[value] => 23
[quantity] => 15
)
)
)
I'm clueless on how to achieve this.
A simple foreach loop over your array to ceate a new array will suffice for this purpose
$new_arr = [];
foreach ($inArray as $arr ) {
$new_arr[$arr['value']][] = $arr;
}
// unset the original array if you are finished with it
// in case it is large and you could make better use
// of the memory for something else
unset($inArray);
If you want to group by the values of multiple keys of the inner arrays, you can join those values together to form the key in your result array.
foreach ($array as $item) {
// combine value and quantity, for example
$key = $item['value'] . '|' . $item['quantity'];
$result[$key][] = $item;
}
just pass an array References and a sort key
function sortBy(&$arr,$by){
$result=array();
foreach($arr as $value){
if(isset($value[$by]))
$result[$value[$by]][]=$value;
}
return $result;
}
Examples
$sorted=sortBy($yourArray,'value'); //by value
$sorted=sortBy($yourArray,'id'); //by Idx
$sorted=sortBy($yourArray,'quantity'); //quantity
You want to group the array keys passing, if I understood correctly.
I usually use the laravel collection library, because it's provided out of the box.
ALthoug, here's my contribution.
Let's try:
function groupArray( $array, $key, $remove = null )
{
$result = array();
foreach (array_unique(array_column($array, $key)) as $value) {
$result[$value] = array_map(function ( $item ) use ( $remove ) {
unset($item[$remove]);
return $item;
}, array_filter($array, function ( $item ) use ( $value, $key ) {
return $item[$key] === $value;
}));
}
return $result;
}
The above function does the job, first we get all the selected key values using the array_column function. THen we do a foreach in the array to filter the array data using the provided key and finally we remove the selected key, if necessary (just because the selected key will be the grouped array keys).
Usage:
$sample = array(
[
'id' => 1,
'value' => 11,
'quantity' => 14
],
[
'id' => 2,
'value' => 11,
'quantity' => 14
],
[
'id' => 3,
'value' => 22,
'quantity' => 14
],
[
'id' => 4,
'value' => 22,
'quantity' => 14
],
[
'id' => 5,
'value' => 23,
'quantity' => 14
],
);
$a = groupArray($sample, 'value', 'value');
$b = groupArray($sample, 'value');
$c = groupArray($sample, 'quantity');
I am working in PHP so I have an array like this, from this array I want filter take user_id to another array like I given below.
Array
(
[0] => Array
(
[user_id] => 66
[distance] => 0
)
[1] => Array
(
[user_id] => 68
[distance] => 0
)
[2] => Array
(
[user_id] => 81
[distance] => 0
)
[3] => Array
(
[user_id] => 65
[distance] => 0.00010218008081861118
)
)
I want an array like this,
$user_id=array(66,68,81,65);
Use array_column()
Returns an array of values representing a single column from the input array.
<?php
$user_array = array(
0 => array('user_id' => 1, 'name' => 'Bob'),
1 => array('user_id' => 2, 'name' => 'John'),
2 => array('user_id' => 3, 'name' => 'Mary')
);
$users = array_column($user_array, 'user_id');
print_r($users);
Output :
Array
(
[0] => 1
[1] => 2
[2] => 3
)
Where $array is the multidimensional array you provided above:
$data = array();
foreach ($array as $item) {
$data[] = $item['user_id'];
}
print_r($data);
I am currently working on a project where the fields scale when clicking on the "Add" button.
I am grouping each field like this: name="packaging[]", name="packaging[1]", name="packaging[2]" and so on. When I submit the form, this is how the data looks like when posted:
Array
(
[packaging] => Array
(
[0] => 1
[1] => 2
)
[quantity] => Array
(
[0] => 1
[1] => 2
)
[total-weight] => Array
(
[0] => 1
[1] => 2
)
[length] => Array
(
[0] => 1
[1] => 2
)
)
Using PHP I would like to convert the above code to look like this:
Array
(
[0] => Array
(
[packaging] => 1,
[quantity] => 1,
[total-weight] => 1,
[length] => 1,
)
[1] => Array
(
[packaging] => 2,
[quantity] => 2,
[total-weight] => 2,
[length] => 2,
)
)
Any help would be greatly appreciated.
Try this....
$array=array();
foreach($data as $key=>$value){
foreach($value as $k=>$val){
$array[$k][$key]=$val;
}
}
DEMO
Try this code:
$rows = array ('packaging' => array ('0'=> 1,'1' => 2),'quantity' => array('0'=> 1,'1' => 2),'total-weight' => array ('0'=> 1,'1' => 2),
'length' =>array ('0'=> 1,'1' => 2)
);
$res_array = array();
$total_records = count($rows['packaging']);
for($i=0;$i<$total_records;$i++)
{
$res_array[] = array('packaging'=>$rows['packaging'] [$i],'quantity'=>$rows['quantity'][$i],
'total-weight'=>$rows['total-weight'][$i],'length'=>$rows['length'] [$i]);
}
print_r($res_array);
I have an array of all possible combinations of values, a bit like working out what monetary values I could make with only certain coins. Now I have an array built but much of the useful data are keys and not values.
A small snippet is below:
Each root key is an array with keys total, denomination and quantity. Each of the quantities multiplied by the denominations total the total. While I've been able to access the total easily enough I just can't get a handle on the denominations and the quantities.
It's my plan to output to separate radio buttons like so:
foreach($array as $arr)
{
echo '<input type="radio" name="name" value="'.$arr[$total].'">';
foreach($arr[denom] as $index => $d)
{
echo $d[qty][$index].' x '.$d[denom][$index].' = '.($qty[$index]*$denom[$index]).'<br>';
}
}
Here's the array I have, any help would be much appreciate, I'm usually great at this bot it's driving me crazy
Array
(
[2] => Array
(
[total] => 105
[denom] => Array
(
[0] => 105
)
[qty] => Array
(
[0] => 1
)
)
[3] => Array
(
[total] => 210
[denom] => Array
(
[0] => 105
)
[qty] => Array
(
[0] => 2
)
)
[4] => Array
(
[total] => 300
[denom] => Array
(
[0] => 300
)
[qty] => Array
(
[0] => 1
)
)
[5] => Array
(
[total] => 405
[denom] => Array
(
[0] => 300
[1] => 105
)
[qty] => Array
(
[0] => 1
[1] => 1
)
)
[6] => Array
(
[total] => 500
[denom] => Array
(
[0] => 500
)
[qty] => Array
(
[0] => 1
)
)
[7] => Array
(
[total] => 605
[denom] => Array
(
[0] => 500
[1] => 105
)
[qty] => Array
(
[0] => 1
[1] => 1
)
)
Constant non-numeric array indices should be written like any other string, i.e. encapsulated in quotes.
foreach($array as $arr) {
echo '<input type="radio" name="name" value="'.$arr['total'].'">';
foreach($arr['denom'] as $index => $d){
for ($j = 0;$j < count($denom);$j++) {
echo $d['qty'][$j].' x '.$d['denom'][$j].' = ';
echo ($d['qty'][$j]*$d['denom'][$j]) . '<br>';
}
}
}
First format your array like this in my code, add 6, 7 array elements. I modify for loop too.
<?php
$array = array(2 => array('total' => 105, 'denom' => array(0 => 105), 'qty' => array(0 => 1)),
3 => array('total' => 210, 'denom' => array(0 => 105), 'qty' => array(0 => 2)),
4 => array('total' => 300, 'denom' => array(0 => 300), 'qty' => array(0 => 1)),
5 => array('total' => 405, 'denom' => array(0 => 300, 1 => 105), 'qty' => array(0 => 1, 1 => 1)),
);
foreach($array as $arr)
{
//var_dump($arr);
echo '<input type="radio" name="name" value="'.$arr['total'].'">';
foreach($arr['denom'] as $index => $d)
{
echo $arr['qty'][$index].' x '.$d.' = '.($arr['qty'][$index]*$d).'<br>';
}
}
?>
I have an array contain this data
Array
(
[id] => Array
(
[0] => 1
[1] => 10
[2] => 4
)
[age] => Array
(
[0] => 1
[1] => 1
[2] => 2
)
)
Now I want to remove duplicates from the ['age'] and leave the first one in tact.
So this would return
Array
(
[id] => Array
(
[0] => 1
[2] => 4
)
[age] => Array
(
[0] => 1
[2] => 2
)
)
Any ideas? Or is there a function already in place to do this?
Like Gordon said, you'd need a custom function to make the relationship but you can use http://php.net/manual/en/function.array-unique.php ?
Wouldn't it be better to have the keys of the age array the corresponding values of the id array?
<?php
$array = array(
'id' => array(0 => 1, 1 => 10, 3 => 4),
'age' => array(0 => 1, 1 => 1, 2 => 2)
);
array_walk($array, 'dupe_killer');
print_r($array);
function dupe_killer(&$value, $key)
{
$value = array_unique($value);
}
?>
You could try this
$array = array('id' => array(1,10,4), 'age'=>array(1,1,2));
$age_array = array();
foreach ($array['age'] as $key => $val) {
if (in_array($val, $age_array))
unset($array['id'][$key], $array['age'][$key]);
$age_array[] = $val;
}
print_r($array);
this returns Array ( [id] => Array ( [0] => 1 [2] => 4 ) [age] => Array ( [0] => 1 [2] => 2 ) )
Regards
Luke