I've a following associative array named $data. Here is some same key value pairs
Array
(
[0] => Array
(
[id] => 1
[config_id] => 31
[language] => "English"
)
[1] => Array
(
[id] => 2
[config_id] => 33
[language] => "English"
)
[2] => Array
(
id] => 3
[config_id] => 32
[language] => "French"
)
)
And i wanted to convert this array as
Array
(
["English"] => Array(
[0]=> Array
(
[id] => 1
[config_id] => 31
)
[1] => Array
(
[id] => 2
[config_id] => 33
)
)
["French"] =>
Array(
[0]=> Array
(
[id] => 3
[config_id] => 32
)
)
)
)
I need the language as key in the output array,Can anyone help me out to resolve this issue? Thanks in advance.
I tried the following code, but printed the last array value only
$arry = array();
foreach ($data as $val) {
$arry[$val->language]["id"] = $val->id;
$arry[$val->language]["config_id"] = $val->config_id;
}
You are nearly there, just need to make the new array all in one go and just use the $arry[$val->language][] to create a new sub array under that new or existing language key.
Also $data is an array or arrays not an array of objects so the addressing of the items was wrong.
$arry = array();
foreach ($data as $val) {
$arry[$val->language][] = ['id' => $val['id'], 'config_id' => $val['config_id']];
}
Is there a way to delete a dimension in a array (only if it's empty), it's pretty dificult to explain with words, so that's what i want to do :
I have an array that returns :
(
[region1] => Array
(
[] => Array
(
[0] => citie1
[1] => citie2
)
[region2] => Array
(
[] => Array
(
[0] => citie1
[1] => citie2
[2] => citie3
)
)
)
I want it to be :
(
[region1] => Array
(
[0] => citie1
[1] => citie2
)
[region2] => Array
(
[0] => citie1
[1] => citie2
[2] => citie3
)
)
foreach($array as $key => $value) {
$array[$key] = reset($value);
}
That will replace each value in the outer array with the first element of that value.
For example I have this array:
Array (
[0] => Array (
[id] => 45 [name] => Name1 [message] => Ololo [date_create] => 21:03:56 )
[1] => Array (
[id] => 46 [name] => visitor [message] => Hi! [date_create] => 21:06:28 )
)
I need converting to:
Array (
[id] => Array (
[0] => 45, [1] => 46
)
[name] => Array (
[0] => Name1, [1] => visitor
)
[message] => Array (
[0] => Ololo, [1] => Hi!
)
[date_create] => Array (
[0] => 21:03:56, [1] => 21:06:28
)
)
I like to know a function for converting this,
Try this block of code:
// Assuming the array you have is called $mainArray.
// The output will be $outputArray.
$outputArray = array();
foreach ($mainArray as $index => $array) { // Iterate through all the arrays inside the main array.
// foreach ($mainArray as $array) { // Use this if the numeric index order doesn't matter.
foreach ($array as $key => $value) { // Iterate through each inner array.
// Load the multidimensional array with the first key as one of (id, name, message, date_create) and second key as the numeric index (if you need it).
$outputArray[$key][$index] = $value;
// $outputArray[$key][] = $value; // Use this if the numeric index order doesn't matter.
}
}
print_r($outputArray);
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);
Found a question which already explains this - How to sort an array of associative arrays by value of a given key in PHP?
I have an array (A1) of arrays (A2). I would like to sort all A2s inside A1 based on a key:value pair in A2
The structure is like this
A1
->A2-1
->K1:Some Value
->K2:ValB
->A2-2
->K1:Other Value
->K2:ValB1
->A2-3
->K1:Some Value
->K2:ValB2
->A2-4
->K1:Other Value
->K2:ValB3
I would like to sort the arrays in A1 so that all A2s for which K1's value is Other Value are bunched together and all A2s for which K1's value is Some Value are bunched together
So after the sorting, the final order of arrays in A1 should be A2-2, A2-4, A2-1, A2-3. Is there a function in PHP using which I can do this? Or do I have to parse through the entire array and do the sorting?
Thanks.
http://php.net/manual/en/function.array-multisort.php
If that doesn't fit, there's a lot of other array functions http://www.php.net/manual/en/ref.array.php
Have a try with:
$A1 = array(
'A2-1' => array(
'K1' => 'Some Value',
'K2' => 'ValB',
),
'A2-2' => array(
'K1' => 'Other Value',
'K2' => 'ValB1',
),
'A2-3' => array(
'K1' => 'Some Value',
'K2' => 'ValB2',
),
'A2-4' => array(
'K1' => 'Other Value',
'K2' => 'ValB3',
)
);
function mySort($a, $b) {
if ($a['K1'] == $b['K1']) {
return strcmp($a['K2'], $b['K2']);
} else {
return strcmp($a['K1'], $b['K1']);
}
}
uasort($A1, 'mySort');
print_r($A1);
output:
Array
(
[A2-2] => Array
(
[K1] => Other Value
[K2] => ValB1
)
[A2-4] => Array
(
[K1] => Other Value
[K2] => ValB3
)
[A2-1] => Array
(
[K1] => Some Value
[K2] => ValB
)
[A2-3] => Array
(
[K1] => Some Value
[K2] => ValB2
)
)
You need something like:
usort($array, function($a, $b)
{
return strcmp($a['k1'], $b['k1']);
});
You may need to replace strcmp with a different sorting function (or operators) because it's unclear exactly what you are doing.
strategy:
1. find unique values, put them aside.
2. loop through unique values
2.1 loop origial array
2.2 store sub array in $out if unique val = original val
<?php
$i=0;
$a3d = array(
$i++ => array(0=>'Some Value',1=>'ValB'),
$i++ => array(0=>'Other Value',1=>'ValB1'),
$i++ => array(0=>'Some Value',1=>'ValB2'),
$i++ => array(0=>'Zome moar',1=>'ValB4'),
$i++ => array(0=>'Other Value',1=>'ValB3'),
$i++ => array(0=>'Zome Moar',1=>'ValB4'),
);
print_r($a3d);
foreach ($a3d as $a2d){
$uniq[]= $a2d[0];
}
$uniq = array_unique($uniq);
sort($uniq);
print_r($uniq);
foreach ($uniq as $v){
foreach ($a3d as $kk => $a2d){
if ($a2d[0] == $v){
$out[]= $a2d;
unset($a3d[$kk]); // <--avoid rechecking elements
}
}
}
print_r(count($a3d));
print_r($out);
?>
$ php sort_array.php
Array
(
[0] => Array
(
[0] => Some Value
[1] => ValB
)
[1] => Array
(
[0] => Other Value
[1] => ValB1
)
[2] => Array
(
[0] => Some Value
[1] => ValB2
)
[3] => Array
(
[0] => Zome moar
[1] => ValB4
)
[4] => Array
(
[0] => Other Value
[1] => ValB3
)
[5] => Array
(
[0] => Zome Moar
[1] => ValB4
)
)
Array
(
[0] => Other Value
[1] => Some Value
[2] => Zome Moar
[3] => Zome moar
)
0Array
(
[0] => Array
(
[0] => Other Value
[1] => ValB1
)
[1] => Array
(
[0] => Other Value
[1] => ValB3
)
[2] => Array
(
[0] => Some Value
[1] => ValB
)
[3] => Array
(
[0] => Some Value
[1] => ValB2
)
[4] => Array
(
[0] => Zome Moar
[1] => ValB4
)
[5] => Array
(
[0] => Zome moar
[1] => ValB4
)
)