Add column of values from one array to another - php

Here is array #1
Array
(
[0] => Array
(
[first] => LightSpeed
[last] => Administrator
)
[1] => Array
(
[first] => Tyler
[last] => Nichol
)
Here is array #2
Array
(
[I-10] => Array
(
[user] => 2
[total] => 46.64
)
[I-11] => Array
(
[user] => 2
[total] => -46.64
)
I just want to add [total] => $value to the first array so it looks like.
Array
(
[0] => Array
(
[first] => LightSpeed
[last] => Administrator
[total] => 46.64
)
[1] => Array
(
[first] => Tyler
[last] => Nichol
[total] => -46.64
)
Pretty sure it is array_push but not sure how to loop it. Any suggestions? Thanks!

It's a bit low tech, but I'd just loop through your array and insert the total item. Assuming you're just matching to the second array on the position of the items in the arrays:
$vals = array_values($arr2);
foreach($arr1 as $i=>$item) {
$arr1[$i]['total'] = $vals[$i]['total'];
}

Your array is not well formatted so I have done that in my answer. You might want to update your question
<?php
$arr1 = Array
(
Array
(
'first' => 'LightSpeed',
'last' => 'Administrator'
),
Array
(
'first' => 'Tyler',
'last' => 'Nichol'
)
);
$arr2 = Array
(
'I-10' => Array
(
'user' => 2,
'total' => 46.64
),
'I-11' => Array
(
'user' => 2,
'total' => -46.64
)
);
$n = count($arr1);
$i = 0;
foreach($arr2 as $arr)
{
$arr1[$i]['total'] = $arr['total'];
$i++;
}
var_dump($arr1);
?>
Result of var_dump
array
0 =>
array
'first' => string 'LightSpeed' (length=10)
'last' => string 'Administrator' (length=13)
'total' => float 46.64
1 =>
array
'first' => string 'Tyler' (length=5)
'last' => string 'Nichol' (length=6)
'total' => float -46.64

You don't have to loop all the time .. array_merge can do the trick
function superMerge($a, $b) {
$a['total'] = $b['total'];
return $a;
}
$array1 = array(0 => Array("first" => "LightSpeed","last" => "Administrator"),1 => Array("first" => "Tyler","last" => "Nichol"));
$array2 = array("I-10" => Array("user" => 2,"total" => 46.64),"I-11" => Array("user" => 2,"total" => - 46.64));
var_dump(array_map("superMerge", $array1, $array2));
Output
array
0 =>
array
'first' => string 'LightSpeed' (length=10)
'last' => string 'Administrator' (length=13)
'total' => float 46.64
1 =>
array
'first' => string 'Tyler' (length=5)
'last' => string 'Nichol' (length=6)
'total' => float -46.64

Related

If in_array on multidimensional array and put value into first array

I have multidimensional array , I just want to push the matched value into another array: Can someone guide me to get the right array as I put into last line.
$getcount= Array
(
0 => Array
(
0 => Array
(
'pickup_trip_location' => 'Bishan',
'trip_route_id' => '1',
'c' => '5'
),
1 => Array
(
'pickup_trip_location' => 'Bukit Merah',
'trip_route_id' => 1,
'c' => 4
),
2 => Array
(
'pickup_trip_location' => 'Geylang',
'trip_route_id' => '1',
'c' => '2'
),
3 => Array
(
'pickup_trip_location' => 'Kallang',
'trip_route_id' => '1',
'c' => '3',
),
) ,
1 => Array
(
0 => Array
(
'pickup_trip_location' => 'Mandai',
'trip_route_id' => '2',
'c' => '2',
),
1 => Array
(
'pickup_trip_location' => 'Queenstown',
'trip_route_id' => '2',
'c' => '3',
),
2 => Array
(
'pickup_trip_location' => 'Toa Payoh',
'trip_route_id' => '2',
'c' => '1'
),
)
);
second array:
$array1_1 = Array
(
0 => array
(
'stoppage_points' => 'Bishan,Bukit Merah,Geylang,Kallang,Toa Payoh,Ang Mo Kio',
),
1 => array
(
'stoppage_points' => 'Queenstown,Toa Payoh,Bedok,Paya Lebar,Mandai,Changi,Yishun',
)
);
$array = [];
//$String = "Bishan,Bukit Merah,Geylang,Kallang,Toa Payoh,Ang Mo Kio";
$f = 0 ;
foreach($array1_1 as $key_1 => $arr){
$one=explode(",",$arr['stoppage_points']);
$i = 0 ;
foreach ($one as $key2=> $item){
$array[$key_1][$key2] = explode(",",$item);
$i++;
}
pr($array);
//die();
foreach($getcount as $key_1 => $arr){
//echo $key_1;
$p = 0 ;
foreach($arr as $key => $arr_1){
echo $key;
$arrayval = $arr_1['pickup_trip_location'];
$c = $arr_1['c'];
$trip_route_id = $arr_1['trip_route_id'];
//echo $id =array_search($arrayval, $array[$key_1][$key][$p]);
if( array_search( $arrayval ,$array[$key_1][$key]) ) {
$array[$key]['pickup_trip_location'] = $arrayval;
$array[$key]['trip_route_id'] = $trip_route_id;
$array[$key]['count'] = $c;
}
}
$p++;
}
pr($array);
$f++;
}
I want to output like this : Getting output for first array only :
Array
(
[0] => Array
(
[0] => Bishan
[pickup_trip_location] => Bishan
[trip_route_id] => 1
[count] => 5
)
[1] => Array
(
[0] => Bukit Merah
[pickup_trip_location] => Bukit Merah
[trip_route_id] => 1
[count] => 4
)
[2] => Array
(
[0] => Geylang
[pickup_trip_location] => Geylang
[trip_route_id] => 1
[count] => 2
)
[3] => Array
(
[0] => Kallang
[pickup_trip_location] => Kallang
[trip_route_id] => 1
[count] => 3
)
[4] => Array
(
[0] => Toa Payoh
)
[5] => Array
(
[0] => Ang Mo Kio
)
)
I have tried to push value into first array but didn't get any luck

Iterate Multilevel Multidimensional Array

I have to iterate multidimensional array with php. My array data as following:
Array
(
[id] => Array
(
['2'] => Array
(
[0] => 2
)
['1'] => Array
(
[0] => 1
)
)
[summary] => Array
(
['2'] => Array
(
[0] => Summary 1
)
['1'] => Array
(
[0] => Summary 2
)
)
[review] => Array
(
['2'] => Array
(
[0] => Review 2
)
['1'] => Array
(
[0] => Summary 2
)
)
[nickname] => ABCD
)
I want to make set of results like array('id','summary','review', 'nickname')
You can try this:
$array = array(
'id' => array(2 => array(2), 1 => array(1)),
'summary' => array(2 => array('Summary ' . 1), 1 => array('Summary ' . 2)),
'review' => array(2 => array('Review ' . 2), 1 => array('Review ' . 2)),
'nickname' => 'ABCD'
);
$out = array();
foreach ($array as $col => $data) {
if (is_array($data)) {
foreach ($data as $key => $value) {
$out[$key][$col] = $value[0];
}
}
}
foreach ($out as &$data) {
$data += array('nickname' => $array['nickname']);
}
var_dump($out);
Output:
array (size=2)
2 =>
array (size=4)
'id' => int 2
'summary' => string 'Summary 1' (length=9)
'review' => string 'Review 2' (length=9)
'nickname' => string 'ABCD' (length=4)
1 => &
array (size=4)
'id' => int 1
'summary' => string 'Summary 2' (length=9)
'review' => string 'Review 2' (length=9)
'nickname' => string 'ABCD' (length=4)

PHP Replace Array Values

I have 2 multidimensional arrays that I am working with:
$arr1 =
Array
([type] => characters
[version] => 5.6.7.8
[data] => Array
([Char1] => Array
([id] => 1
[name] =>Char1
[title] =>Example
[tags] => Array
([0] => DPS
[1] => Support))
[Char2] => Array
([id] => 2
[name] =>Char2
[title] =>Example
[tags] => Array
([0] => Tank
[1] => N/A)
)
)
etc...
$arr2=
Array
([games] => Array
([gameId] => 123
[gameType => Match
[char_id] => 1
[stats] => Array
([damage] => 55555
[kills] => 5)
)
([gameId] => 157
[gameType => Match
[char_id] => 2
[stats] => Array
([damage] => 12642
[kills] => 9)
)
etc...
Basically, I need almost all the data in $arr2... but only the Char name from $arr1. How could I merge or add the $arr1['name'] key=>value into $arr2 where $arr1['id'] is equal to $arr2['char_id'] as the "id" field of each array is the same number.
I've attempted using array_merge and array_replace, but I haven't come up with any working solutions. This is also all data that I am receiving from a 3rd party, so I have no control on initial array setup.
Thanks for any help or suggestions!
Actually, this is quite straighforward. (I don't think there a built-in function that does this.)
Loop $arr2 and under it loop also $arr1. While under loop, just add a condition that if both ID's match, add that particular name to $arr2. (And use some referencing & on $arr2)
Consider this example:
// your data
$arr1 = array(
'type' => 'characters',
'version' => '5.6.7.8',
'data' => array(
'Char1' => array(
'id' => 1,
'name' => 'Char1',
'title' => 'Example',
'tags' => array('DPS', 'Support'),
),
'Char2' => array(
'id' => 2,
'name' => 'Char2',
'title' => 'Example',
'tags' => array('Tank', 'N/A'),
),
),
);
$arr2 = array(
'games' => array(
array(
'gameId' => 123,
'gameType' => 'Match',
'char_id' => 1,
'stats' => array('damage' => 55555, 'kills' => 5),
),
array(
'gameId' => 157,
'gameType' => 'Match',
'char_id' => 2,
'stats' => array('damage' => 12642, 'kills' => 9),
),
),
);
foreach($arr2['games'] as &$value) {
$arr2_char_id = $value['char_id'];
// loop and check against the $arr1
foreach($arr1['data'] as $element) {
if($arr2_char_id == $element['id']) {
$value['name'] = $element['name'];
}
}
}
echo '<pre>';
print_r($arr2);
$arr2 should look now like this:
Array
(
[games] => Array
(
[0] => Array
(
[gameId] => 123
[gameType] => Match
[char_id] => 1
[stats] => Array
(
[damage] => 55555
[kills] => 5
)
[name] => Char1 // <-- name
)
[1] => Array
(
[gameId] => 157
[gameType] => Match
[char_id] => 2
[stats] => Array
(
[damage] => 12642
[kills] => 9
)
[name] => Char2 // <-- name
)
)
)
Iterate over $arr2 and add the data to it from the matching $arr1 array value:
$i = 0;
foreach($arr2['games'] as $arr2Game){
$id = $arr2Game['char_id'];
$arr2['games'][$i]['name'] = $arr1['data'][$id]['name'];
$i++;
}
Have not tested this code.
If I'm understanding you correctly, you want to add a name index to each of the arrays within the $arr2['games'] array.
foreach($arr2['games'] as $key => $innerArray)
{
$arr2['games'][$key]['name'] = $arr1['data']['Char'.$innerArray['char_id']]['name'];
}

removing parent array and pairing up its inner arrays values

here is my array
$array = Array
(
[0] => Array
(
[name] => crud_inputs[id]
[value] => id_Value
)
[1] => Array
(
[name] => crud_inputs[user_id][]
[value] => userid_Value
)
[2] => Array
(
[name] => crud_inputs[details]
[value] => details_value
)
)
i want to remove parent array ( $array ) and pair the
[name]=[value]
in each inner array
i want to end up with a
crud_inputs array
i.e
$crud_inputs[id] = id_Value ;
$crud_inputs[user_id][] = userid_Value ;
$crud_inputs[details] = details_value ;
-
I WANT TO BE ABLE TO DO SOMETHING LIKE THIS AT THE END
$MY_ORM->UPDATE( $table , $crud_inputs );
this is what i wrote so far but it doesn't work , i get a empty array at the end
$crud_inputs = array();
foreach($array as $ar )
{
$$ar['name'] = $ar['value'];
}
var_dump($crud_inputs);
#Wrikken , =========================================================
this is exactly what i get as my raw array
array (size=6)
0 =>
array (size=2)
'name' => string 'crud_inputs[id]' (length=15)
'value' => string 'id_Value' (length=8)
1 =>
array (size=2)
'name' => string 'crud_inputs[user_id][]' (length=22)
'value' => string 'userid_Value' (length=3)
2 =>
array (size=2)
'name' => string 'crud_inputs[user_id][]' (length=22)
'value' => string 'userid_Value2' (length=3)
3 =>
array (size=2)
'name' => string 'implode[user_id]' (length=16)
'value' => string ',' (length=1)
4 =>
array (size=2)
'name' => string 'crud_inputs[date]' (length=18)
'value' => string 'date_value' (length=13)
5 =>
array (size=2)
'name' => string 'crud_inputs[ip]' (length=15)
'value' => string 'ip_value' (length=13)
-
and this is how i handle it
$new = array();
foreach($array as $value){
$new[$value['name']] = $value['value'];
}
$item = $new;
var_dump($item);
This what was meant as a GET or POST array? parse_str helps:
$array = ...your array...
foreach($array as &$item){
$item = array_map('urlencode',$item);
$item = implode('=',$item);
}
$string = implode('&',$array);
parse_str($string,$result);
var_dump($result);
//and if I read the rest right:
foreach($result['implode'] as $key =>$value){
$result['crudinputs'][$key] = implode($value,$result['crudinputs'][$key]);
}
Earlier on:
Bad oneliner (your colleagues will hate you):
foreach($array as &$item){
$item = call_user_func_array('array_combine',call_user_func_array('array_merge_recursive',$item));
}
Readable multiliner (everybody happy):
foreach($array as &$item){
$new = array();
foreach($item as $value){
$new[$value['name']] = $value['value'];
}
$item = $new;
}
This is assuming an array with multiple items if I read your code correctly, so like:
Array
(
[0] => Array
(
[0] => Array
(
[name] => key0
[value] => value0
)
[1] => Array
(
[name] => key1
[value] => value1
)
[2] => Array
(
[name] => key2
[value] => value2
)
[3] => Array
(
[name] => key3
[value] => value3
)
)
[1] => Array
.... more items here
If not (you only have 1 item), you can omit the outer foreach loop in both examples.

Merge arrays (PHP)

How combine arrays in this way?
source:
Array
(
[0] => Array
(
[id] => 3
[title] => book
[tval] => 10000
)
[1] => Array
(
[id] => 3
[title] => book
[tval] => 1700
)
[3] => Array
(
[id] => 27
[title] => fruit
[tval] => 3000
)
.......
)
result:
Array
(
[0] => Array
(
[id] => 3
[title] => book
[tval] => 10000,1700
)
[1] => Array
(
[id] => 27
[title] => fruit
[tval] => 3000
)
.......
)
please help to solve this problem,
thanks!!!
sorry for bad english(
This should work:
$result = array();
foreach($array as $elem) {
$key = $elem['id'];
if (isset($result[$key])) {
$result[$key]['tval'] .= ',' . $elem['tval'];
} else {
$result[$key] = $elem;
}
}
This basically groups elements by id, concatenating tvals (separated by ,).
Simply building slightly on user576875's method:
$a = array ( 0 => array ( 'id' => 3,
'title' => 'book',
'tval' => 10000
),
1 => array ( 'id' => 3,
'title' => 'book',
'tval' => 1700
),
3 => array ( 'id' => 27,
'bcalias' => 'fruit',
'tval' => 3000
)
);
$result = array();
foreach ($a as $elem) {
$key = $elem['id'];
if (isset($result[$key])) {
$result[$key]['tval'] .= ',' . $elem['tval'];
} else {
$result[$key] = $elem;
}
}
$result = array_merge($result);
var_dump($result);
gives a result of:
array
0 =>
array
'id' => int 3
'title' => string 'book' (length=4)
'tval' => string '10000,1700' (length=10)
1 =>
array
'id' => int 27
'bcalias' => string 'fruit' (length=5)
'tval' => int 3000
The only real difference is the array_merge() to reset the keys

Categories