i have array like :
$options = array (
'af' => 'Afrikaans',
'af_NA' => 'Afrikaans (Namibië)',
'af_ZA' => 'Afrikaans (Suid-Afrika)',
);
i want to add key for the country and for the id like :
$options = array (
[0]=> array (
'id' => 'af'
'name' => 'Afrikaans',
),
[1]=> array (
'id' => 'af_NA'
'name' => 'Afrikaans (Namibie)',
),
)
Make an empty array and then push new arrays onto it after you map your new values correctly.
$new = array();
foreach ($options as $key => $value) {
array_push($new, array("id" => $key, "name" => $value));
}
$options = $new;
You could use a map with lambda:
$result = array_map(function ($id, $name) { return array('id' => $id,'name' => $name); }, array_keys($options), $options);
var_dump($result);
Output:
array (size=3)
0 =>
array (size=2)
'id' => string 'af' (length=2)
'name' => string 'Afrikaans' (length=9)
1 =>
array (size=2)
'id' => string 'af_NA' (length=5)
'name' => string 'Afrikaans (Namibië)' (length=20)
2 =>
array (size=2)
'id' => string 'af_ZA' (length=5)
'name' => string 'Afrikaans (Suid-Afrika)' (length=23)
Related
What I have:
array(
[
'id' => 12, 'group' => 'abc', 'name' => 'Lorem',
],
[
'id' => 12, 'group' => 'def', 'name' => 'Ipsum',
],
[
'id' => 34, 'group' => 'ghi', 'name' => 'Dolor',
],
[
'id' => 34, 'group' => 'jkl', 'name' => 'Sit',
],
[
'id' => 34, 'group' => 'mno', 'name' => 'Amet',
],
);
What I want:
array (size=2)
12 =>
array (size=2)
'abc' =>
array (size=3)
'id' => int 12
'group' => string 'abc' (length=3)
'name' => string 'Lorem' (length=5)
'def' =>
array (size=3)
'id' => int 12
'group' => string 'def' (length=3)
'name' => string 'Ipsum' (length=5)
34 =>
array (size=3)
'ghi' =>
array (size=3)
'id' => int 34
'group' => string 'ghi' (length=3)
'name' => string 'Dolor' (length=5)
'jkl' =>
array (size=3)
'id' => int 34
'group' => string 'jkl' (length=3)
'name' => string 'Sit' (length=3)
'mno' =>
array (size=3)
'id' => int 34
'group' => string 'mno' (length=3)
'name' => string 'Amet' (length=4)
What I tried:
The obvious:
$sorted = array();
foreach ($products as $product) {
$sorted[$product['id']][$product['group']] = $product;
}
$products = $sorted;
unset($sorted);
But then, in an effort to avoid foreach() loops, and with help of #atomrc's answer to How to group subarrays by a column value?, I came up with this:
$sorted = array_reduce($products, function ($accumulator, $element) {
$accumulator[$element['id']][] = $element;
return $accumulator;
}, []);
array_walk($sorted, function(&$item) {
$item = array_reduce($item, function ($accumulator, $element) {
$accumulator[$element['group']] = $element;
return $accumulator;
}, []);
});
So while the above looks cool and all, it's also much bigger and seemingly more complex than that foreach. This is probably because my experience with array_reduce() is limited. Is there a way to have the array grouped in one go? For example, in the first array_reduce() call.
Personally I don't see an issue with the foreach solution, but if you want to use array_reduce you can simply use the inner code from the foreach loop:
$grouped = array_reduce($products, function ($c, $v) {
$c[$v['id']][$v['group']] = $v;
return $c;
}, []);
print_r($grouped);
The output of this code is the same as your foreach loop.
Note that this (and your foreach loop) won't deal correctly with the situation where there is more than one array with the same id and group values. For example, if we extend $products:
$products[] = [
'id' => 34, 'group' => 'jkl', 'name' => 'Consectetur',
];
Then the first jkl group value gets overwritten by the second. To deal with this you need to make each group an array e.g.
$grouped = array_reduce($products, function ($c, $v) {
$c[$v['id']][$v['group']][] = $v;
return $c;
}, []);
print_r($grouped);
In this case the jkl group then looks like:
[jkl] => Array
(
[0] => Array
(
[id] => 34
[group] => jkl
[name] => Sit
)
[1] => Array
(
[id] => 34
[group] => jkl
[name] => Consectetur
)
)
Demo on 3v4l.org
array (size=1)
11 =>
array (size=1)
'subject' =>
array (size=1)
'Maths' => string '21' (length=2)
array (size=1)
10 =>
array (size=1)
'subject' =>
array (size=1)
'Maths' => string '10' (length=2)
array (size=1)
11 =>
array (size=1)
'subject' =>
array (size=1)
'Gujarati' => string '21' (length=2)
array (size=1)
10 =>
array (size=1)
'subject' =>
array (size=1)
'Gujarati' => string '20' (length=2)
array key duplicate like 11 and 10
MY Question:
How to Create Below output array .
array (size=1)
11 =>
array (size=1)
'subject' =>
array (size=1)
'Maths' => string '21' (length=2)
'Gujarati' => string '21' (length=2)
array (size=1)
10 =>
array (size=1)
'subject' =>
array (size=1)
'Maths' => string '10' (length=2)
'Gujarati' => string '20' (length=2)
Use this function to get your result.
Btw: Just asking for help again and again without investigating by yourself is bad behavior, if someone points you to a function (like array_merge_recursive) you should at least take some time to RTM.
function array_merge_recursive_new() {
$arrays = func_get_args();
$base = array_shift($arrays);
foreach ($arrays as $array) {
reset($base); //important
while (list($key, $value) = #each($array)) {
if (is_array($value) && #is_array($base[$key])) {
$base[$key] = array_merge_recursive_new($base[$key], $value);
} else {
$base[$key] = $value;
}
}
}
return $base;
}
Source/Copyright:
http://php.net/manual/en/function.array-merge-recursive.php#106985
I have tested it with these arrays, which should be the same as you have them.
$arr1 = array(
10 => array( "subject" => array("math" => 1)),
11 => array( "subject" => array("math" => 2)),
);
$arr2 = array(
10 => array( "subject" => array("guawhatever" => "foo")),
11 => array( "subject" => array("guawhatever" => "bar")),
);
$blubb = array_merge_recursive_new($arr1,$arr2);
I have an array like this:
Array (
[0] => 10060127
[1] => 10065127
[2] => 10070127
[3] => 10075127
)
I want to add an associative array based on the value in the array. e.g. find 10070127 and an an associative array to it containing various other info. Something like:
[1] => 10065127 => Time : 10:00
Date : 16/12/2014
Count : 1
How can I recognise the correct position and push these items to this array?
You can try this:
$dataArray = array(
0 => 10060127,
1 => 10065127,
2 => 10070127,
3 => 10075127
);
$toAdd = array(
1 => array(
10065127 => array("Time" => '10:00',
"Date" => '16/12/2014',
"Count" => '1'
)
),
2 => array(
10070127 => array("Time" => '17:25',
"Date" => '11/12/2014',
"Count" => '95'
)
)
);
foreach ($toAdd as $subArray) {
$toSearch = key($subArray);
$pos = array_search($toSearch, $dataArray);
if ($pos !== false) {
unset($dataArray[$pos]);
$dataArray[$toSearch] = $subArray[$toSearch];
}
}
var_dump ($dataArray);
Output will be:
array
0 => int 10060127
3 => int 10075127
10065127 =>
array
'Time' => string '10:00' (length=5)
'Date' => string '16/12/2014' (length=10)
'Count' => string '1' (length=1)
10070127 =>
array
'Time' => string '17:25' (length=5)
'Date' => string '11/12/2014' (length=10)
'Count' => string '95' (length=2)
Something like:
$id = '10070127';
$array[array_search($id, $array)] = array($id=>array(
'time'=>'10:00',
'Date'=>'16/12/2014',
'Count'=>1));
I’m trying to add to an array in a loop but only the first element in the loop is added.
The array
array (size=7)
0 =>
array (size=2)
'id' => int 1
'name' => string 'john' (length=11)
1 =>
array (size=2)
'id' => int 2
'name' => string 'adam' (length=13)
2 =>
array (size=2)
'id' => int 3
'name' => string 'mary' (length=11)
My loop
foreach ($loops as $key => $loop) {
$idArray['id'] = $loop['id'];
}
var_dump($idArray); die();
Did I do anything wrong?
You overwrite your old value by assigning the new value to the array. An array can't have identical keys.
Try this:
foreach ($loops as $key => $loop)
{
$idArray['id'][] = $loop['id'];
}
var_dump($idArray); die();
So you add items to an array inside your array.
If you want the ordinal values of the $idarray to be the primary key values of what you are iterating over you can do this.
$loops = array(array('id' => 1, 'name' => 'john'), /* ... */);
foreach ($loops as $key => $loop)
{
$idArray[$loop['id']] = $loop;
}
var_dump($idArray); die();
var_dump will reveal this structure
array (size=7)
1 =>
array (size=2)
'id' => int 1
'name' => string 'john' (length=4)
2 =>
array (size=2)
'id' => int 2
'name' => string 'adam' (length=4)
3 =>
array (size=2)
'id' => int 3
'name' => string 'mary' (length=4)
I have this array:
array (size=3)
0 =>
array (size=2)
'name' => string 'XML' (length=3)
'processer' => string 'XMLp' (length=12)
1 =>
array (size=2)
'name' => string 'XML2' (length=3)
'processer' => string 'XML2pr' (length=12)
2 =>
array (size=2)
'name' => string 'CSV' (length=3)
'processer' => string 'CSVp' (length=12)
Since I dont need all of this, I wasnt this array converted:
$a = array ('XML', 'XML2', 'CSV');
so get by 'name'. How to do this elegantly in php?
$source = array(
0 => array (
'name' =>'A',
'processer' => 'XMLf'),
1 => array (
'name' =>'B',
'processer' => 'XMLp'),
2 => array (
'name' =>'C',
'processer' => 'XMLp')
);
$output = array_map(function ($value) {
return $value['name'];
}, $source);
print_r($output);
You could just loop over it, I don't think there's a much more elegant way:
$a = array();
foreach ($array as $value) {
$a[] = $value ['name'];
}
foreach($array as $a) $new[] = $a['name'];