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?
Related
I am trying to convert following array:
array (size=6)
0 =>
array (size=1)
1 => string '611' (length=3)
1 =>
array (size=1)
1 => string '610' (length=3)
2 =>
array (size=1)
1 => string '608' (length=3)
3 =>
array (size=1)
1 => string '607' (length=3)
4 =>
array (size=1)
1 => string '606' (length=3)
5 =>
array (size=1)
1 => string '605' (length=3)
Expected output: 611, 610, 608, 607, 606, 605
I tried to do this:
foreach ($array as $sub) {
$str = implode(',', $sub);
}
but I got 605
Could you explain what I am doing wrong
In your code there is one error in foreach. You always replace previous value, you just need to do:
foreach ($array as $sub) {
$str .= implode(',', $sub);
}
I wish to add string keys to my inner PHP arrays. So, I want to convert this:
array (size=2)
0 => array (size=3)
0 => string 'X705' (length=4)
1 => string 'X723' (length=4)
2 => string 'Sue' (length=0)
1 => array (size=3)
0 => string 'X714' (length=4)
1 => string 'X721' (length=4)
2 => string 'John' (length=0)
to this:
array (size=2)
0 =>
array (size=3)
'code1' => string 'X705' (length=4)
'code2' => string 'X723' (length=4)
'name' => string 'Sue' (length=0)
1 =>
array (size=3)
'code1' => string 'X714' (length=4)
'code2' => string 'X721' (length=4)
'name' => string 'John' (length=0)
I think I need to use array_walk but cannot fathom it out. Any help appreciated.
You can use array_map for that purpose:
$newarray = array_map(function($x) {
return array("code1" => $x[0], "code2" => $x[1], "name" => $x[2]);
}, $array);
where $array is your input array.
Start with this:
foreach ($array as $key=>$item) {
$item['code1']=$item[0];
unset($item[0]);
$item['code2']=$item[1];
unset($item[1]);
$item['name']=$item[2];
unset($item[2]);
$array[$key]=$item;
}
I would use array_map() but here's an alternate:
foreach($array as &$v) {
$v = array_combine(array('code1','code2','name'), $v);
}
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;
}
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'];