I have the following array:
array (size=2)
'filename' =>
array (size=2)
0 => string 'f1' (length=2)
1 => string 'f2' (length=2)
'url' =>
array (size=2)
0 => string 'u1' (length=2)
1 => string 'u2' (length=2)
I want to map it like so:
array (size=2)
0 =>
array (size=2)
'filename' => string 'f1' (length=2)
'url' => string 'u1' (length=2)
1 =>
array (size=2)
'filename' => string 'f2' (length=2)
'url' => string 'u2' (length=2)
How would I write such a map without knowing what the keys are called (filename and url). Would it also be possible for larger arrays?
How about this:
foreach ($startArray as $key1 => $value1) {
foreach ($value1 as $key2 => $value2) {
$endArray[$key2][$key1] = $value2;
}
}
This solution will be flexible enough to deal with arrays of any size.
With your array as $startArray:
$result = array();
$counter = 0;
while ( count ( array_values($startArray)[0] ) < $counter - 1 ) {
$result[] = add_array( $startArray, $counter );
$counter++;
}
function add_array( $array, $index ) {
$result = array();
foreach ( $array as $array_key => $array_value ) {
$result[$array_key] = $array_value[$index];
}
return $result;
}
Related
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);
Source/Input
I have a multidimensional array:
array (size=2)
'array_one' =>
array (size=2)
0 =>
array (size=2)
0 => string 'ABC' (length=3)
1 => string 'ABC' (length=3)
'myid' =>
array (size=2)
0 => string 'ABC' (length=3)
1 => string 'ABC' (length=3)
'array_two' =>
array (size=2)
0 =>
array (size=2)
0 => string 'DEF' (length=3)
1 => string 'DEF' (length=3)
'myid' =>
array (size=2)
0 => string 'DEF' (length=3)
1 => string 'DEF' (length=3)
i want to combine the arrays but if an array element's key is the same i want to replace both element with a new element with a new (dynamically created using it's parent) name, i want the output to look something like this:
Expected Output
array (size=1)
'new_array' =>
array (size=2)
'myid_array_one' =>
array (size=2)
0 => string 'ABC' (length=3)
1 => string 'ABC' (length=3)
'myid_array_two' =>
array (size=2)
0 => string 'DEF' (length=3)
1 => string 'DEF' (length=3)
Method
This is what i have tried:
public function data_combine($data_set_arrays) {
$result = [];
$accociative = $data_set_arrays;
foreach($accociative as &$array) {
foreach($array as $key => $value) {
if(is_int($key)) {
unset($array[$key]);
}
}
}
foreach($accociative as $array) {
//make sure no elements have same name
$parent = key($array);
foreach($array as $key => $value) {
$duplicates = array_intersect_key($data_set_arrays, $array); {
foreach($duplicates as $key => $value) {
$array[$key.'_'.$parent] = $array[$key];
unset($array[$key]);
}
}
}
$result = array_merge_recursive($result, $array);
}
return $result;
}
but i can't see the wood for the trees, can someone lend a hand?
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)
So I have this array which the vardump looks like this:
array (size=4)
0 =>
array (size=1)
'field_4' =>
array (size=1)
0 => string 'item-1' (length=6)
1 =>
array (size=1)
'field_4' =>
array (size=1)
0 => string 'info-1' (length=6)
2 =>
array (size=1)
'field_5' =>
array (size=1)
0 => string 'item-2' (length=6)
3 =>
array (size=1)
'field_5' =>
array (size=1)
0 => string 'info-2' (length=6)
So I am trying to combine the array with the same key for example 'field_4' would be merge/combined into an array that has 2 items "item-1" and "info-1".
So the end result I would like is this:
array (size=2)
0 =>
array (size=1)
'field_4' =>
array (size=2)
0 => string 'item-1' (length=6)
1 => string 'info-1' (length=6)
1 =>
array (size=1)
'field_5' =>
array (size=1)
0 => string 'item-2' (length=6)
1 => string 'info-2' (lenght=6)
So is there a PHP convenience function to handle this or do I have to rebuild the array?
Thanks for looking.
Just iterate over the input array, building the merged array:
$merged = array();
foreach ($input as $a)
foreach ($a as $k => $v)
foreach ($v as $v2)
$merged[$k][] = $v2;
And then flatten it into your weird required output:
$flattened = array();
foreach ($merged as $k => $v)
$flattened[] = array($k => $v);
Input:
$input = array(
array('field_4' => array('item-1')),
array('field_4' => array('info-1')),
array('field_5' => array('item-2')),
array('field_5' => array('info-2')),
);
Output:
array(
array('field_4' => array('item-1', 'info-1')),
array('field_5' => array('item-2', 'info-2')),
)
When dumping output, print_r or var_export make for a much more readable example than var_dump
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'];