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
)
Related
I've been stuck on this for the better part of the day and I'm out of ideas. I have an array like this:
Array
(
[rank] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[name] => Array
(
[0] => 'Hideki'
[1] => 'Rory'
[2] => 'Sam'
[money] => Array
(
[0] => '$100'
[1] => '$200'
[2] => '$500'
)
)
and I have the task to create an array with the following format from it:
Array
(
[Hideki] => Array
(
[rank] => 1
[money] => '$100'
)
[Rory] => Array
(
[rank] => 2
[money] => '$200'
[Sam] => Array
(
[rank] => 3
[money] => '$500'
)
)
The catch is that 'rank' and 'money' have to be dynamic names
It should be simple as that:
$new = [];
foreach($array['name'] as $key => $name) {
$new[$name] = [
'rank' => $array['rank'][$key],
'money' => $array['money'][$key]
];
}
Little late but here my answear. My approach was to use the array_walk() function.
$array = [
'rank' => [1,2,3],
'name' => ['Hideki', 'Rory', 'Sam'],
'money' => ['$100', '$200', '$500'],
];
$i = 0;
$newArray = [];
array_walk($array['name'], function($name) use (&$i, $array, &$newArray) {
$newArray[$name] = ['rank'=> $array['rank'][$i], 'money' => $array['money'][$i]];
$i++;
});
print_r($newArray);
Run your first array through a foreach loop referencing only the "name" key and using key=>value pairs. Then reference the other keys from the first array when you build the new array, setting the value as the key to the second array.
You will need to first get the keys using array_keys() and use a nested foreach to loop through all the keys.
Example:
$keys1 = array_keys($array1);
foreach ($array1['name'] as $key => $value) {
$val2 = array();
foreach ($keys1 as $k){
if ($k != 'name') $val2[$k] = $array1[$k][$key];
}
$array2[$value] = $val2;
}
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
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
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 2 db queries that return values from the db, what's the easiest way to do this. Is there a PHP function that can do this or do I have to use a loop ?
$names = array(
'det-1' => array('foo'),
'det-2' => array('bar'),
'det-3' => array('doe')
);
$emails = array(
'det-1' => array('foo#gmail.com'),
'det-2' => array('bar#gmail.com'),
'det-3' => array('doe#gmail.com')
);
// the result
$details = array(
'det-1' => array('foo', 'foo#gmail.com')
'det-2' => array('bar', 'bar#gmail.com')
'det-3' => array('doe', 'doe#gmail.com')
);
Use array_merge_recursive():
php > print_r(array_merge_recursive($names, $emails));
Array
(
[det-1] => Array
(
[0] => foo
[1] => foo#gmail.com
)
[det-2] => Array
(
[0] => bar
[1] => bar#gmail.com
)
[det-3] => Array
(
[0] => doe
[1] => doe#gmail.com
)
)
You can use array_merge_recursive
$newArray = array_merge_recursive($names, $emails);