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'];
Related
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)
I have the following array (contents retrieved from a DB):
array (size=4)
0 =>
array (size=2)
'number' => string 'one' (length=3)
'name' => string 'Billy' (length=5)
1 =>
array (size=2)
'number' => string 'two' (length=3)
'name' => string 'Mariah' (length=6)
2 =>
array (size=2)
'number' => string 'three' (length=5)
'name' => string 'Cindy' (length=5)
3 =>
array (size=2)
'number' => string 'four' (length=4)
'name' => string 'Daniel' (length=6)
I need to create an array as follows:
$info = array('one' => 'Billy', 'two' => 'Mariah', 'three' => 'Cindy', 'four' => 'Daniel');
I used a foreach loop to construct the desired array:
$info = array();
foreach ($result as $row) {
$info[] = array($row['number'] => $row['name']);
}
and a var_dump() gives me this instead:
array (size=4)
0 =>
array (size=1)
'one' => string 'Billy' (length=5)
1 =>
array (size=1)
'two' => string 'Mariah' (length=6)
2 =>
array (size=1)
'three' => string 'Cindy' (length=5)
3 =>
array (size=1)
'four' => string 'Daniel' (length=6)
How can I achieve the desired array in PHP? Thanks for your help in advance.
You were close.
$info = array();
foreach ($result as $row) {
$info[$row['number']] =$row['name'];
}
With PHP >= 5.5.0 it's much easier:
$info = array_column($result, 'name', 'number');
Or use the PHP Implementation of array_column()
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 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