Reputation: 609
I need to restructure a multidimensional array in a Drupal application.
Here is full image:
http://imageshack.us/photo/my-images/405/array.png/
field_new => [...]
has to become 'value' => '11111'
Upvotes: -1
Views: 133
Reputation: 48031
I recommend directly modifying the input array by using array destructuring syntax to isolate the data
subarray as a reference variable, then declare the value
to be the deepest scalar value of field_new
.
Code: (Demo)
foreach ($array as ['data' => &$data]) {
$data['value'] = $data['field_new']['und'][0]['value'];
unset($data['field_new']);
}
var_export($array);
Upvotes: 0
Reputation: 1966
Your Input Array:
...
0{
data{
nid => 1
vid => 1
type => article
field_new{
und{
0{
value => 11111
}
}
}
}
}
1{
data{
nid => 2
vid => 2
type => article
field_new{
und{
0{
value => 33333
}
}
}
}
}
Your desired array:
...
0{
data{
nid => 1
vid => 1
type => article
value => 11111
}
}
1{
data{
nid => 2
vid => 2
type => article
value => 33333
}
}
Unset (http://php.net/manual/en/function.unset.php) is our friend.
unset() destroys the specified variables.
Use this code:
<?php
$loop_count = count($input_arr);
for($i=0;$i<loop_count;$i++){
//copy the value to the desired place
$input_arr[$i]["data"]["value"] = $input_arr[$i]["data"]["field_new"]["und"][0]["value"];
//delete the unwanted portion
unset($input_arr[$i]["data"]["field_new"]);
} // for loops ENDS
?>
Assumptions:
Please add the code you are using you to produce/get this array, and we could provide you more specific answers.
Upvotes: 1