Iterate Multilevel Multidimensional Array - php

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)

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

PHP Array Search and Delete

I have two array:
Array 1:
Array ( [0] => Array ( [id] => et1 [supplier_id] => 4 [supplier_product_code] => 00054X [is_active] => 1 )
[1] => Array ( [id] => et2 [supplier_id] => 4 [supplier_product_code] => 000558 [is_active] => 1 )
[2] => Array ( [id] => et3 [supplier_id] => 5 [supplier_product_code] => 00054X [is_active] => 1 ));
Array 2:
Array ( [0] => Array ([id] => et1 [same_sku] => et3);
I need to delete all the same_skus in array1 from array2.
So from my result array I need array1 to be:
Array ( [0] => Array ( [id] => et1 [supplier_id] => 4 [supplier_product_code] => 00054X [is_active] => 1 )
[1] => Array ( [id] => et2 [supplier_id] => 4 [supplier_product_code] => 000558 [is_active] => 1 ));
Code that I have right now does not work.
public function search_array($array, $val)
{
foreach ($array as $key => $row)
{
if ($row['id'] === $val)
{
return $key;
}
}
}
foreach($array2->result() as $row)
{
$id = $row->id;
$same_sku = $row->same_sku;
$key = $this->search_array($array1, $id);
if(!empty($key))
{
$same_sku_key = $this->search_array($array1, $same_sku);
if(!empty($same_sku_key))
unset($array1[$same_sku_key]);
}
}
In the following code I have recreated the two arrays from your example. I then created a function that removes from a haystack array (array1) all of the sub arrays that have an "id" that matches the value of "same_sku" within a needle array (array2). The final line echos the result array.
EDIT
I have modified the original answer to pass the array values by reference and unset the unwanted sub arrays, instead of passing by value, looping, and returning another array. This should resolve the memory issue, as well as the other issue mentioned in your comment.
$array1 = array(
array(
'id' => 'et1',
'supplier_id' => '4',
'supplier_product_code' => '00054X',
'is_active' => '1'
),
array(
'id' => 'et2',
'supplier_id' => '4',
'supplier_product_code' => '000558',
'is_active' => '1'
),
array(
'id' => 'et3',
'supplier_id' => '5',
'supplier_product_code' => '00054X',
'is_active' => '1'
),
array(
'id' => 'et4',
'supplier_id' => '5',
'supplier_product_code' => '00054X',
'is_active' => '1'
)
);
$array2 = array(
array(
'id' => 'et1',
'same_sku' => 'et3'
),
array(
'id' => 'et2',
'same_sku' => 'et4'
)
);
function remove_same_sku(&$haystack, &$needles){
foreach($needles as $needle){
foreach($haystack as $key => $val){
if($val['id'] === $needle['same_sku']){
unset($haystack[$key]);
}
}
}
}
remove_same_sku($array1, $array2);
echo print_r($array1);

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.

Add column of values from one array to another

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

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