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.
Related
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)
I am getting this array when form is submitted
array
'item' =>
array
0 => string 'Salt'
1 => string 'Pepper'
'quantity' =>
array (size=2)
0 => string '2 spoon'
1 => string '5'
and now want to rearrange above array, so it should look like
array
'0' =>
array
'item' => string 'Salt'
'quantity' => string '2 spoon'
'1' =>
array
'item' => string 'Pepper'
'quantity' => string '5'
I tried so many combinations but failed, will somebody help me how to rearrange this array. Any help will be more than appreciated.
Try this
$array = array(
'item' =>
array(
0 => 'Salt',
1 => 'Pepper'),
'quantity' =>
array(
0 => '2 spoon',
1 => '5')
);
$new_array = array();
foreach ($array['item'] as $key => $value) {
$new_array[$key]["item"] = $value;
$new_array[$key]["quantity"] = $array['quantity'][$key];
}
echo "<pre>";
var_dump($new_array);
Do it as below -
<?php
$arr=array(
'item' =>
array(
0 => 'Salt' ,
1 => 'Pepper'
),
'quantity' =>
array (
0 =>'spoon',
1 =>'5'
)
);
$result=array();
$com=array_combine($arr['item'],$arr['quantity']);
foreach($com as $k=>$v)
{
$result[]=array("item"=>$k,"quantity"=>$v);
}
print_r($result);
?>
OUTPUT
Array (
[0] => Array ( [item] => Salt [quantity] => spoon )
[1] => Array ( [item] => Pepper [quantity] => 5 )
)
try this, I think this would help you,
$a = array(
'item' =>
array(
0 => 'Salt',
1 => 'Pepper'),
'quantity' =>
array(
0 => '2 spoon',
1 => '5')
);
$i = 0;
foreach($a['item'] as $row){
$b[$i]["item"] = $row;
$b[$i]["quantity"] = $a['quantity'][$i];
$i++;
}
print_r($b);
Output from print_r() will be
Array
(
[0] => Array
(
[item] => Salt
[quantity] => 2 spoon
)
[1] => Array
(
[item] => Pepper
[quantity] => 5
)
)
You can simply use array_walk like as
$result = [];
array_walk($arr['item'], function($v, $k)use(&$result, $arr) {
$result[$k]['item'] = $arr['item'][$k];
$result[$k]['quantity'] = $arr['quantity'][$k];
});
print_r($result);
Demo
Hoping that your array is stored in the $array variable and that item is leading. Meaning that there is always an item in the array and not always a quantity
<?php
foreach($array['item'] as $key => $item) {
$newArray[$key]['item'] = $item;
if (isset($array['quantity'][$key]) {
$newArray[$key]['quantity'] = $array['quantity'][$key];
} else {
$newArray[$key]['quantity'] = 0;
}
?>
I have an array like this
Array
(
[name] => Array
(
[0] => img/test240.jpg
[1] => img/cs1.jpg
[2] => img/cs2.jpg
[3] => img/cs3.jpg
)
[link] => Array
(
[0] => http://google.com
[1] => http://google.com
[2] => http://facebook.com
[3] => http://orkut.com
)
[order] => Array
(
[0] => 4
[1] => 1
[2] => 2
[3] => 3
)
)
I need to sort it by order WHICH IS KEY in Multidimensional array. Here is output.
Array
(
[name] => Array
(
[1] => img/cs1.jpg
[2] => img/cs2.jpg
[3] => img/cs3.jpg
[0] => img/test240.jpg
)
[link] => Array
(
[1] => http://google.com
[2] => http://facebook.com
[3] => http://orkut.com
[0] => http://google.com
)
[order] => Array
(
[1] => 1
[2] => 2
[3] => 3
[0] => 4
)
)
In this you can see when order is sorted name and link is also sorted according to the order. How can i do this with php.
You have to use array_map() in conjunction with sort().
If you want to preserve actual element order you have to use asort() instead sort().
Try this code:
$arr = array(
'name' => array(
0 => 'img/test240.jpg',
1 => 'img/cs1.jpg',
2 => 'img/cs2.jpg',
3 => 'img/cs3.jpg',
),
'link' => array(
0 => 'http://google.com',
1 => 'http://google.com',
2 => 'http://facebook.com',
3 => 'http://orkut.com',
),
'order' => array(
0 => 4,
1 => 1,
2 => 2,
3 => 3,
),
);
function mysort($a) {
asort($a);
return $a;
}
$arr = array_map('mysort', $arr);
print_r($arr);
Demo.
Try this, it uses array_multisort:
$array holds:
array (size=3)
'name' =>
array (size=4)
0 => string 'img/test240.jpg' (length=15)
1 => string 'img/cs1.jpg' (length=11)
2 => string 'img/cs2.jpg' (length=11)
3 => string 'img/cs3.jpg' (length=11)
'link' =>
array (size=4)
0 => string 'http://google.com' (length=17)
1 => string 'http://google.com' (length=17)
2 => string 'http://facebook.com' (length=19)
3 => string 'http://orkut.com' (length=16)
'order' =>
array (size=4)
0 => string '4' (length=1)
1 => string '1' (length=1)
2 => string '2' (length=1)
3 => string '3' (length=1)
Code:
$sort = array();
foreach($array as $k) {
foreach($k as $ind=>$val){
$sort['name'][$ind] = $array['name'][$ind];
$sort['link'][$ind] = $array['link'][$ind];
$sort['order'][$ind] = $array['order'][$ind];
}
}
array_multisort($sort['order'], SORT_ASC, $sort['link'], SORT_ASC, $sort['name'], SORT_ASC);
var_dump($sort);
Output:
array (size=3)
'name' =>
array (size=4)
0 => string 'img/cs1.jpg' (length=11)
1 => string 'img/cs2.jpg' (length=11)
2 => string 'img/cs3.jpg' (length=11)
3 => string 'img/test240.jpg' (length=15)
'link' =>
array (size=4)
0 => string 'http://google.com' (length=17)
1 => string 'http://facebook.com' (length=19)
2 => string 'http://orkut.com' (length=16)
3 => string 'http://google.com' (length=17)
'order' =>
array (size=4)
0 => string '1' (length=1)
1 => string '2' (length=1)
2 => string '3' (length=1)
3 => string '4' (length=1)
$this_arr = array(1,2,3,0);
function my_sort_2($arr, $arrangement)
{
$flag = false;
foreach($arr as $key => $val)
{
if(is_array($arr[$key]))
{
$arr[$key] = my_sort_2($arr[$key],$arrangement);
$flag = true;
}
}
if($flag == false && is_array($arr) && is_assoc($arr) === false)
{
$temp = array();
for($i = 0; $i < count($arrangement); $i++)
{
if(isset($arr[$arrangement[$i]]))
{
$temp[$arrangement[$i]] = $arr[$arrangement[$i]];
unset($arr[$arrangement[$i]]);
}
}
//$arr = array_merge($temp,$arr);
$arr = $temp;
}
return $arr;
}
Include this function below to run my own function. Also credit to #Matt Whittingham where i got this code from
function is_assoc($array)
{
$keys = array_keys($array);
return array_keys($keys) !== $keys;
}
Now let's do some sortin'... print_r(my_sort_2($arr,$this_arr)); assuming $arr contains Shan's array.
The output is EXACTLY what you desired.
It'll search for nested array (at least intended) and see if it's in a standard numeric ordered keys (in short, not custom order - yet; and not assoc) then sort it the way you want.
Note: I know my code isn't that probably good, optimized or bug free and that's my second attempt, misunderstanding your requirements first time (see the function name?).
Well after some research i found a simple solution like this
asort($data['order']);
$keys = array_keys($data['order']);
$data['name'] = array_replace(array_flip($keys), $data['name']);
$data['link'] = array_replace(array_flip($keys), $data['link']);
$data['order'] = array_replace(array_flip($keys), $data['order']);
Although i dont want to apply array_replace and array_flip on all the keys but this is done for the time being. I will surely trying to find how i can do it with single instruction.
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
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