This is my array,with the use of array_column or any loop I want to replace keys as value of next element .I don't want to change parent array index
Array
(
[0] => Array
(
[id] => 11
[total] => 100000
[content] => abc
)
[1] => Array
(
[id] => 22
[total] => 200000
[content] => def
)
)
This is the array I would like to have.
Array
(
[0] => Array
(
[11] => 100000
[content] => abc
)
[1] => Array
(
[22] => 200000
[content] => def
)
)
It is very simple Try this:-
$array = array(
'0'=>array('id'=> 10,'total'=> 100000,'content' => 'abc'),
'1'=>array('id'=> 11,'total'=> 200000,'content' => 'def')
);
foreach($array as $key => $val){
$array[$key][$val['id']] = $val['total'];
unset($array[$key]['total']);
unset($array[$key]['id']);
}
echo "<pre>"; print_r($array); die; // print array data here
Hope it helps!
Using 'foreach' to modify existing array works perfect, but 'array_walk' is just more expressive, because it was designed for this task.
<?php
$array = [
[
'id' => 11,
'total' => 10000,
'content' => 'abc'
],
[
'id' => 22,
'total' => 200000,
'content' => 'def'
]
];
array_walk($array, function(&$row) {
$row[$row['id']] = $row['total'];
unset($row['id'], $row['total']);
});
var_dump($array);
It can be done with array_map function
$new = array_map(function($x) {
return [ $x['id']=> $x['total'], 'content' => $x['content']]; },
$array);
demo
Related
I would like to know how to combine and overwrite the already existed array value in php.
my current array look like:
Array
(
[1149] => 3
[4108] => 5
)
As shown above values, i need expected result in php as below :
Array
(
[0] => Array
(
[offer_id] => 1149
[quantity] => 3
)
[1] => Array
(
[offer_id] => 4108
[quantity] => 5
)
)
How about:
$result = [];
foreach (
[
1149 => 3,
4108 => 3,
] as $key => $value
) {
$result[] = [
'offer_id' => $key,
'quantity' => $value,
];
}
print_r($result);
Sorry if there's an obvious answer, but I've been trying for hours to google the solution and tried various ways. Basically I'm trying to shift an array within a multidimensional array one level to the right. The output array is to be fed into a program which only accepts the desired format of multidimensional array.
We have ($result):
Array (
[0] => Array (
[id] => 1
[data] => AAA
)
[1] => Array (
[id] => 2
[data] => BBB
)
[2] => Array (
[id] => 3
[data] => CCC
)
)
We want to insert a new array [test] in between the multidimensional array. Desired result:
Array (
[0] => Array (
[test] => Array (
[id] => 1
[data] => AAA
)
)
[1] => Array (
[test] => Array (
[id] => 2
[data] => BBB
)
)
[2] => Array (
[test] => Array (
[id] => 3
[data] => CCC
)
)
)
What I've got so far:
foreach ($result as $key => $value) {
$newarray[] = array($key => $value)
};
return $newarray;
Unfortunately the above inserts an incremental index instead of [test] where we want it to.
Can anyone help?
You could use a loop and index in the same array using the current key. Then update the value with a new array where using test as the key and $v as the value.
$arrays = [
0 => [
"id" => 1,
"data" => "AAA"
],
1 => [
"id" => 2,
"data" => "BBB"
],
2 => [
"id" => 3,
"data" => "CCC"
],
];
foreach ($arrays as $k => $v) {
$arrays[$k] = ['test' => $v];
};
print_r($arrays);
Result
Array
(
[0] => Array
(
[test] => Array
(
[id] => 1
[data] => AAA
)
)
[1] => Array
(
[test] => Array
(
[id] => 2
[data] => BBB
)
)
[2] => Array
(
[test] => Array
(
[id] => 3
[data] => CCC
)
)
)
Demo
Try this:
foreach ($result as $key => $value) {
$newarray[$key] = ['test' => $value];
};
This would be a working and flexible approach:
<?php
$data = [
[
'id' => 1,
'data' => "AAA"
],
[
'id' => 2,
'data' => "BBB"
],
[
'id' => 3,
'data' => "CCC"
]
];
array_walk($data, function(&$entry) {
$entry = [
'test' => $entry
];
});
print_r($data);
The output obviously is:
Array
(
[0] => Array
(
[test] => Array
(
[id] => 1
[data] => AAA
)
)
[1] => Array
(
[test] => Array
(
[id] => 2
[data] => BBB
)
)
[2] => Array
(
[test] => Array
(
[id] => 3
[data] => CCC
)
)
)
$data = [
[
'id' => 1,
'data' => "AAA"
],
[
'id' => 2,
'data' => "BBB"
],
[
'id' => 3,
'data' => "CCC"
]
];
$newArray = array_map(function ($value) {
return ['test' => $value];
}, $data);
var_dump($newArray);
https://secure.php.net/manual/en/function.array-map.php
I have an array like below: all the values I am getting one array only, but I don't want this way. This is the best way to do so, in php or jQuery both languages are ok for me
Array
(
[0] => Array
(
[val] => facebook
)
[1] => Array
(
[val] => snapchat
)
[2] => Array
(
[val] => instagram
)
[3] => Array
(
[expenses] => 986532
)
[4] => Array
(
[expenses] => 45456
)
[5] => Array
(
[expenses] => 56230
)
[6] => Array
(
[social_id] => 15
)
[7] => Array
(
[social_id] => 16
)
[8] => Array
(
[social_id] => 17
)
)
and I want to output like this..
$result = array(
array(
"val" => "Facebook",
"expenses" => "84512",
"social_id" => 1
),
array(
"val" => "Instagram",
"expenses" => "123",
"social_id" => 2
)
);
but this should be dynamic, the length of the above array can be varies
You can merge the arrays to one and use array_column to get all vals or expenses in a separate array.
Then foreach one array and build the new array with the key.
//$a = your array
// Grab each column separate
$val = array_column($a, "val");
$expenses= array_column($a, "expenses");
$social_id = array_column($a, "social_id");
Foreach($val as $key => $v){
$arr[] = [$v, $expenses[$key], $social_id[$key]];
}
Var_dump($arr);
https://3v4l.org/nVtDf
Edit updated with the 'latest' version of the array. My code works just fine with this array to without any changes.
to get that array style you can do it by hand for each array
function test(array ...$arr)
{
$result = array();
foreach ($arr as $key => $pair) {
foreach ($pair as $item => $value) {
$result[$item] = $value;
}
}
return $result;
}
$arr1 = test( ['facebook' => 1], ['expenses' => 100]);
print_r($arr1);
/* Array (
[facebook] => 1,
[expenses] => 100
)
*/
$arr2 = test( $arr1, ['more info' => 'info'] );
print_r($arr2);
/* Array (
[facebook] => 1,
[expenses] => 100,
[more info] => info
)
*/
you can pass it any number of
['Key' => 'Pair']
array('Key' => 'Pair')
or even a previous made array and it will add them up into 1 big array and return it
I have an array that looks like this:
getting array need to convert array as same key value as 0
foreach($array as $key=>$id){
$consumer_data[]=$this->App_model->get_session($id);
}
print_r($consumer_data);
Array
(
[0] => Array
(
[0] => Array
(
[ConsumerID] => 1
[name] => asdfd
)
[1] => Array
(
[ConsumerID] => 5
[name] => test
)
[2] => Array
(
[ConsumerID] => 3
[name] => test1
)
)
[1] => Array
(
[0] => Array
(
[ConsumerID] => 4
[name] => test4
)
)
i want to implement array like this in same key value as 0
Array
(
[0] => Array
(
[0] => Array
(
[ConsumerID] => 1
[name] => asdfd
)
[1] => Array
(
[ConsumerID] => 5
[name] => test
)
[2] => Array
(
[ConsumerID] => 3
[name] => test1
)
[3] => Array
(
[ConsumerID] => 4
[name] => test4
)
)
I am using PHP. Can anyone point me to a good starting point as to how I should go about doing this?
You can use array_merge():
$new_array[0] = array_merge($array[0], $array[1]);
Where $array is the first array.
SEE DEMO
OR for a more dynamic approach:
$new_array = array(0 => array());
foreach($array as $a) {
$new_array[0] = array_merge($new_array[0], $a);
}
SEE DEMO 2
The simpliest solution is to do it with:
$input = array(
array(
array('ConsumerID' => 1, 'name' => 'asdfd'),
array('ConsumerID' => 5, 'name' => 'test'),
array('ConsumerID' => 4, 'name' => 'test1'),
),
array(
array('ConsumerID' => 4, 'name' => 'test4'),
),
);
$output = array(
array()
);
foreach ($input as $data) {
$output[0] = array_merge($output[0], $data);
}
Try this->
$newArray = array();
foreach($values as $key=>$val){
$newArray [0][$key]=$val;
}
print_r($newArray);
Check this:
<?php
$arr[0] = array(0 => array("ConsumerID" => 1, "name" => "Ni"), 1 => array("ConsumerID" => 2, "name" => "Ab"));
$arr[1] = array(1 => array("ConsumerID" =>5, "name" => "GE"), 1 => array("ConsumerID" => 6, "name" => "DB"));
$new = array();
foreach($arr as $key => $value) {
foreach($value as $innerkey => $innervalue) {
$new[0][] = $innervalue;
}
}
print_r($new);
?>
I have an array like this.
Array
(
[0] => Array
(
[email] => abc#gmail.com
[timestamp] => 2013-05-03 09:20:01
)
[1] => Array
(
[email] => def#gmail.com
[timestamp] => 2013-05-03 09:20:23
)
[2] => Array
(
[email] => ghi#gmail.com
[timestamp] => 2013-05-03 09:20:43
)
)
I want this to be as simple as like this.
Array
(
[0] => abc#gmai1.com
[1] => def#gmail.com
[2] => ghi#gmail.com
)
I have tried unset function but it still doesn't work as i expected.
I am not big into array concept and hence my stupid questions !!! :(
I think that it'd be better to use array_map instead of unset:
function filter($x)
{
return $x['email'];
}
$emails = array_map('filter', $your_array);
This basically will map your input array into output array using filter function.
foreach($foo as $key=>$value)
{
$foo[$key] = $value['email'];
}
Simply pick from your old array what you want and put it in a new one.
$newArray = array();
foreach($oldArray as $containedArray)
{
$newArray[] = $containedArray['email'];
}
var_dump($newArray);
<?php
$array = Array
(
Array
(
email => 'abc#gmail.com',
timestamp => '2013-05-03 09:20:01'
),
Array
(
email => 'def#yahoo.co.in',
timestamp => '2013-05-03 09:20:23'
),
Array
(
email => 'ghi#gmail.com',
timestamp => '2013-05-03 09:20:43'
),
);
foreach ($array as $array){
$newArray[] = $array['email'];
}
var_dump($newArray);
There is no need to unset anything. Just assign the appropriate value:
<?php
$array = array(
array(
"email" => 'aaa#aaa.com',
"timestamp" => 2,
),
array(
"email" => 'bbb#aaa.com',
"timestamp" => 3,
),
);
foreach($array as $key => $value)
{
$array[$key] = $value["email"];
}
var_dump($array);
Try this,
<?php
$array= array(
0 => array
(
'email' => 'abc#gmail.com',
'timestamp' =>' 2013-05-03 09:20:01'
),
1 => array
(
'email'=> 'def#gmail.com',
'timestamp' => '2013-05-03 09:20:23'
),
2 => array
(
'email'=> 'ghi#gmail.com',
'timestamp' => '2013-05-03 09:20:43'
)
);
foreach($array as $key => $data)
{
$array[$key]=$data['email'];
}
print_r($array);
?>
You will get
Array
(
[0] => abc#gmai1.com
[1] => def#gmail.com
[2] => ghi#gmail.com
)