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);
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];
}
Here is the array:
[cart] => Array
(
[ProductId] => Array
(
[0] => P121100001
[1] => P121100002
)
[SellerId] => Array
(
[0] => S12110001
[1] => S12110001
)
[SpecifyId] => Array
(
[0] => 1
[1] => 2
)
[Quantity] => Array
(
[0] => 1
[1] => 1
)
[Price] => Array
(
[0] => 12
[1] => 29
)
[TotalPrice] => 41
)
I have the ProductId and I want to remove all the other items matching P121100002's key.
Is there an easy way to do this I can't can seem to come up with one?
You can loop through the full array and use unset() to, well, "unset" the specified index:
$index = array_search($cart['ProductId'], 'P121100002');
if ($index !== false) {
foreach ($cart as $key => $arr) {
unset($cart[$key][$index]);
}
}
The slight caveat to this approach is that it may disrupt your index orders. For instance, say you have:
[ProductId] => Array (
[0] => P121100001
[1] => P121100002
[2] => P121100003
)
And you want to remove P121100002, which has a corresponding index of 1. Using unset($cart['ProductId'][1]) will cause your array's to become:
[ProductId] => Array (
[0] => P121100001
[2] => P121100003
)
This may be something to remain concerned with if you're going to use a for loop to iterate through in the future. If it is, you can use array_values() to "reset" the indexes in the unset() loop from above:
foreach ($cart as $key => $arr) {
unset($cart[$key][$index]);
$cart[$key] = array_values($cart[$key]);
}
foreach($yourArray['ProductId'] as $key => $value) {
if ($value == $productIdToRemove) {
foreach($yourArray as $deleteKey => $deleteValue) {
unset($yourArray[$deleteKey][$key]);
}
break;
}
}
Use array_key_exists along with the unset() function
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);