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);
Related
I have an array like this:
Array
(
[0] => Array
(
[id] => 10
[field] => new
[value] => pqr
)
[1] => Array
(
[id] => 14
[field] => test
[value] => abc
)
[2] => Array
(
[id] => 17
[field] => test
[value] => xyz
)
)
Now I want to merge this array with field name with id and value will be comma separated. So my new array will look like:
Array
(
[0] => Array
(
[id] => 10
[field] => new
[value] => pqr
)
[1] => Array
(
[id] => 14,17
[field] => test
[value] => abc,xyz
)
)
Can we do this with any php inbuilt function.
I don't know of any built in function to do this, but it's pretty trivial with a simple foreach loop.
String Concatenation Approach
$new_array = [];
foreach($array1 as $arr) {
$field = $arr['field'];
$id = $arr['id'];
$value = $arr['value'];
//we use $field as $new_array keys so we can combine values
if(!array_key_exists($field, $new_array)) {
//key doesn't exist in new array, so create it
$new_array[$field] = $arr;
} else {
//key exists in new array, append new values
$new_array[$field]['id'] .= ",{$id}";
$new_array[$field]['value'] .= ",{$value}";
}
}
//reset array keys back to sequential
$new_array = array_values($new_array);
Output of $new_array would be
Array
(
[0] => Array
(
[id] => 10
[field] => new
[value] => pqr
)
[1] => Array
(
[id] => 14,17
[field] => test
[value] => abc,xyz
)
)
Normalized Array Approach
$new_array = [];
foreach($array1 as $arr) {
$field = $arr['field'];
$id = $arr['id'];
$value = $arr['value'];
//we use $field as $new_array keys so we can combine values
if(!array_key_exists($field, $new_array)) {
//key doesn't exist in new array, so create it
$new_array[$field] = ['id' => [$id], 'field' => $field, 'value' => [$value]];
} else {
//key exists in new array, append new values
$new_array[$field]['id'][] = $id;
$new_array[$field]['value'][] = $value;
}
}
//reset array keys back to sequential
$new_array = array_values($new_array);
Output of $new_array would be
Array
(
[0] => Array
(
[id] => Array
(
[0] => 10
)
[field] => new
[value] => Array
(
[0] => pqr
)
)
[1] => Array
(
[id] => Array
(
[0] => 14
[1] => 17
)
[field] => test
[value] => Array
(
[0] => abc
[1] => xyz
)
)
)
I have two array. array one and array two. I want to merge these array into single array with key. My output result is valid but sequence is not correct.
Array one
Array
(
[0] => test-685f1e7bc357187e449479d627100102
[1] => test-685f1e7bc357187e449479d627d29390
)
Array two
Array
(
[0] => DF955298-A664-4FA7-9586-FCD4CF977777
[1] => DF955298-A664-4FA7-9586-FCD4CF988888
)
Expected Result
Array
(
[0] => Array
(
[key] => test-685f1e7bc357187e449479d627100102
[uuid] => DF955298-A664-4FA7-9586-FCD4CF977777
)
[1] => Array
(
[key] => test-685f1e7bc357187e449479d627d29390
[uuid] => DF955298-A664-4FA7-9586-FCD4CF988888
)
)
My code is for that result is mentioned below:
$record = array();
foreach ($keys_array as $key => $all_key) {
foreach ($uuid_array as $uuid_key => $all_uuid) {
$record[$key]['key'] = $all_key;
$record[$uuid_key]['uuid'] = $all_uuid;
}
}
My output sequence is not valid. Where is the problem
Array
(
[0] => Array
(
[key] => test-685f1e7bc357187e449479d627100102
[uuid] => DF955298-A664-4FA7-9586-FCD4CF977777
)
[1] => Array
(
[uuid] => test-685f1e7bc357187e449479d627d29390
[key] => DF955298-A664-4FA7-9586-FCD4CF988888
)
)
Simple solution:
$record = array();
foreach ($keys_array as $key => $all_key) {
$record[] = [
'key' => $all_key,
// get value under the same key from `$uuid_array`
'uuid' => $uuid_array[$key],
];
}
I got a PHP multidimensional array, I want to create variables from it first element as variable name and the second element as the variable value. I want to use this logic to print create the variables based on the language selected, the first column always will have the same names but the second value will generate different strings based on the language selected.
Array
(
[0] => Array
(
[0] => el1
[1] => Grouping
)
[1] => Array
(
[0] => el2
[1] => Type
)
[2] => Array
(
[0] => el3
[1] => Starting Date
)
[3] => Array
(
[0] => el4
[1] => Ending Date
)
[4] => Array
(
[0] => el5
[1] => Section
)
[5] => Array
(
[0] => el6
[1] => Cell
)
[6] => Array
(
[0] => el7
[1] => Client
)
[7] => Array
(
[0] => el8
[1] => Status
)
[8] => Array
(
[0] => el9
[1] => Article
)
[9] => Array
(
[0] => el10
[1] => Search
)
)
I want to assign the [0] value as a variable name and [1] as the variable value, the declaration should be in this way related to my array presented before:
<?php
el1="Grouping";
el2="Type";
el3="Starting Date";
?>
... and so on.
I want to echo out on the HTML page the string from the variable.
Try this :
foreach ($array as $index => $subarray) {
${$subarray[0]} = $subarray[1];
}
You loop throught your big array
You get the first value with key 0 and transform it as variable : see documentation
You assign the value with key 1 and assign it to your variable
Test :
$array = array(
0 => array(
0 => "test",
1 => "value"
),
1 => array(
0 => "test2",
1 => "value2"
)
);
foreach ($array as $index => $subarray) {
${$subarray[0]} = $subarray[1];
}
var_dump($test, $test2);
The output is :
string 'value' (length=5)
string 'value2' (length=6)
A simple loop through the array should do the trick:
foreach ($data as $item) {
$temp = $item[0];
${$temp} = $item[1];
}
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);
How would I go about flipping an array and establishing relationships between all the values and keys? For example:
I am trying to turn this:
Array (
[11913] => Array (
[0] => 4242
[1] => 3981
)
[9878] => Array (
[0] => 2901
[1] => 3981
)
[11506] => Array (
[0] => 3981
[1] => 2901
)
)
Into this:
Array (
[3981] => Array (
[0] => 11506
[1] => 9878
[2] => 11913
)
[2901] => Array (
[0] => 11506
[1] => 9878
)
[4242] => Array (
[0] => 11913
)
)
Are there any PHP functions that will already do this automatically? If not what would be a way of going about this? Can't seem to wrap my head around it.
Here you go.
$final_array = array();
foreach($initial_array as $key => $val){
foreach($val as $v){
$final_array[$v][] = $key;
}
}