This question already has answers here:
PHP Change Array Keys
(12 answers)
Closed 8 years ago.
I have an array of arrays, something like this:
Array
(
[0] => Array
(
[0] => DC1F180E-FE57-622C-28AE-8194843B4D84
[1] => First Choice
[2] => 1
)
[1] => Array
(
[0] => EB877F3C-7A3B-98A7-9240-580FB797030A
[1] => Second Choice
[2] => 0
)
[2] => Array
(
[0] => D3C0EA56-73D2-C7E3-8236-EEA2400DFA9C
[1] => Third Choice
[2] => 0
)
)
How do I can rename all the keys in "nested" arrays to get something like this in all "nested" arrays:
...
[1] => Array
(
[id] => EB877F3C-7A3B-98A7-9240-580FB797030A
[name] => Second Choice
[status] => 0
)
...
This way you can change keys in your array:
for ($i=0, $c = count($array); $i<$c; ++$i) {
$array[$i]['id'] = $array[$i][0];
$array[$i]['name'] = $array[$i][1];
$array[$i]['status'] = $array[$i][2];
unset($array[$i][0];
unset($array[$i][1];
unset($array[$i][2];
}
You have to use syntax $array[$key1][$key2] to use multidimensional array.
You could use array_walk and array_combine like this:
$a = array(array('foo','bar'),array('foo','bar'));
print_r($a);
$keys = array('first','second');
$new_array = array();
array_walk($a,function($x) use (&$new_array,$keys) {
$new_array[] = array_combine($keys,$x);
});
print_r($new_array);
array_walk goes through each element of your array, applying a callback function. array_combine combines an array of keys with an array of values to produce a new array.
output:
Array ( [0] => Array ( [0] => foo [1] => bar )
[1] => Array ( [0] => foo [1] => bar ) )
Array ( [0] => Array ( [first] => foo [second] => bar )
[1] => Array ( [first] => foo [second] => bar ) )
Related
I am trying to remove duplicate and empty values from array with array_unique() function, but getting wrong output.
Data:
Array (
[0] => Array (
[0] =>
[1] => 1
[2] =>
[3] => 108
[4] =>
)
[1] => Array (
[0] =>
[1] => 1
[2] =>
[3] => 108
[4] =>
[5] => 101
)
[2] => Array (
[0] =>
[1] =>
[2] => 108
[3] =>
)
)
PHP:
$array = array_filter($userids);
$arrays = array_unique($array, SORT_REGULAR);
print_r($arrays);
nothing happens with SORT_REGULAR - output comes same as raw data, and without SORT_REGULAR this output is coming:
$array = array_filter($userids);
$arrays = array_unique($array);
print_r($arrays);
output:
Array (
[0] => Array
(
[0] =>
[1] => 1
[2] =>
[3] => 108
[4] =>
)
)
output I am looking for:
Array (
[0] => Array
(
[0] => 1
[1] => 108
[2] => 101
)
)
Those array functions only works on a single level. If you flatten the array (adding all elements in a single element array), it should be pretty straight forward.
Flatten the array
$array = array_merge(...$array);
Note: This method works fine for flattening indexed arrays like in your example, but not for associative arrays where any of the sub arrays contains the same keys.
Then filter out all empty
$array = array_filter($array);
and then remove all duplicates
$array = array_unique($array);
Or as a one-liner:
$array = array_unique(array_filter(array_merge(...$array)));
Demo: https://3v4l.org/pEJAJ
I have an array, which is given below:
$test = Array
(
[0] => Array
(
[0] => stud 1
)
[1] => Array
(
[0] => stud 2
)
[2] => Array
(
[0] => stud 3
)
);
I want to add a common element to above array with out using loop. For example, I want to add "test" to each element of array. After adding "test", array will look like:
$test = Array
(
[0] => Array
(
[0] => stud 1
[1] => 'test'
)
[1] => Array
(
[0] => stud 2
[1] => 'test'
)
[2] => Array
(
[0] => stud 3
[1] => 'test'
)
);
Is there any way to add common element array with out using any kind of loop(for, foreach etc...)?
You can use array_map(), check the live demo
array_map(function($v){$v[] = 'test'; return $v;}, $array);
This question already has answers here:
Transposing multidimensional arrays in PHP
(12 answers)
Closed 5 months ago.
I have a two arrays and need to get one from them. array_merge, array_map etc. Doesn't give right result.
$array1 = [1,2,3,4,5];
$array2 = [a,b,c,d,e];
I need $array3 = [[1,a], [2,b], [3,c]]...
What is the best way to get that result?
using array_map(null, $arr1, $arr2) you can achieve the result.
http://php.net/manual/en/function.array-map.php
php > $q = array_map(null, $array1, $array2);
php > print_r($q);
Array
(
[0] => Array
(
[0] => 1
[1] => a
)
[1] => Array
(
[0] => 2
[1] => b
)
[2] => Array
(
[0] => 3
[1] => c
)
[3] => Array
(
[0] => 4
[1] => d
)
[4] => Array
(
[0] => 5
[1] => e
)
)
The for loop approach is the following:
<?php
$array1 = [1,2,3,4,5];
$array2 = ['a','b','c','d','e'];
$array3 = array();
if(count($array1) == count($array2))
{
for($i = 0; $i < count($array1); $i++)
{
$array3[] = [$array1[$i],$array2[$i]];
}
}
else
{
die("SIZE MISMATCH");
}
echo '<pre>';
print_r($array3);
And the output is:
Array
(
[0] => Array
(
[0] => 1
[1] => a
)
[1] => Array
(
[0] => 2
[1] => b
)
[2] => Array
(
[0] => 3
[1] => c
)
[3] => Array
(
[0] => 4
[1] => d
)
[4] => Array
(
[0] => 5
[1] => e
)
)
I have this array:
SimpleXMLElement Object
(
[id] => Array
(
[0] => Koala.jpg
[1] => Jellyfish.jpg
)
[desc] => Array
(
[0] => koaladesc
[1] => jelly desc
)
[qtidade] => Array
(
[0] => 1
[1] => 5
)
I need create some php function that help me group the values like this:
[0] => Array
(
[0] => Koala.jpg
[1] => koaladesc
[2] => 1
)
[1] => Array
(
[0] => Jellyfish
[1] => jelly desc
[2] => 5
)
Could anyone help me?
Something like this should do the trick, but it's localized to what you're asking based on the vagueness of your question:
$new_array = array();
foreach($simple_xml_object as $obj) {
if(is_array($obj)) {
for($i = 0; $i < count($obj); $i++) {
$new_array[$i][] = $obj[$i];
}
}
}
I would suggest looking at the documentation on the foreach() construct, as well as looking over the SimpleXML manual.
So, you want to tranpose an array. Here's a magical way of transposing rectangular arrays:
array_unshift($array, null);
$array = call_user_func_array('array_map', $array);
let's suppose your array is saved in variable $arrayValues
$arrayValues = [id] => Array
(
[0] => Koala.jpg
[1] => Jellyfish.jpg
)
[desc] => Array
(
[0] => koaladesc
[1] => jelly desc
)
[qtidade] => Array
(
[0] => 1
[1] => 5
)
now you need to create the following code:
foreach($arrayValues as $array)
{
echo $array->id;
echo $array->desc;
echo $array->qtidade;
}
this may work well for you.
i have a multidimensional array whose index/keys (not the values) are like this:
this is how the submitted array looks
[param] => Array
(
[3] => groupedlista
[0] => groupedlistb
[2] => groupedlistc
)
[f_name] => Array
(
[3] => grouplistaa
[0] => grouplistbb
[2] => grouplistcc
)
[f_label] => Array
(
[3] => grouplistL3
[0] => grouplistL0
[2] => grouplistL2
)
this is how the order looks
0,2,3
i want that Result
[param] => Array
(
[0] => groupedlistb
[1] => groupedlistc
[2] => groupedlista
)
[f_name] => Array
(
[0] => grouplistbb
[1] => grouplistcc
[2] => grouplistaa
)
[f_label] => Array
(
[0] => grouplistL0
[1] => grouplistL2
[2] => grouplistL3
)
that's it
PS: i use a jquery sort / add / delete feature in the form and i prefer to do the final sorting php-based. the index array [$i] is required to be declared at the form.
$order = '0,2,3';
$out = array(); // This will hold the sorted values
$order = explode(',',$order); // Turn the order into an array
foreach ($multiDimArray as $key => $subArray) { // Loop outer array
foreach ($order as $pos) { // Loop order array
if (isset($subArray[$pos])) { // Make sure the key exists
$out[$key][] = $subArray[$pos]; // Put the correct value in the correct place
}
}
}
print_r($out);