I have one array in two format. I want to change array from
Array
(
[step_number] => 4
[app_id] => Array
(
[0] => 2
[1] => 3
)
[formdata] => Array
(
[0] => Array
(
[name] => app_id[]
[value] => 2
)
[1] => Array
(
[name] => app_id[]
[value] => 3
)
[2] => Array
(
[name] => fieldval[2][2][]
[value] => 1
)
[3] => Array
(
[name] => fieldval[3][3][]
[value] => 200
)
[4] => Array
(
[name] => fieldval[3][3][]
[value] => day
)
[5] => Array
(
[name] => title
[value] => new plan
)
[6] => Array
(
[name] => feature_plan
[value] => 3
)
[7] => Array
(
[name] => plan_type
[value] => free
)
[8] => Array
(
[name] => price
[value] =>
)
[9] => Array
(
[name] => sell_type
[value] => us
)
)
)
this format to
Array
(
[app_id] => Array
(
[0] => 2
[1] => 3
)
[fieldval] => Array
(
[2] => Array
(
[2] => Array
(
[0] => 1
)
)
[3] => Array
(
[3] => Array
(
[0] => 200
[1] => day
)
)
)
[title] => new plan
[feature_plan] => 3
[plan_type] => free
[price] =>
[sell_type] => us
)
these are are one array into two format. i have data in to first array format and i want to change that format to second array type format.
please tell me how i am trying this for 2 days but not succeed.
Here is a function you could use to produce that conversion:
function convert_formdata($input) {
$output = array();
foreach($input['formdata'] as $data) {
$keys = preg_split("#[\[\]]+#", $data['name']);
$value = $data['value'];
$target = &$output;
foreach($keys as $key) {
// Get index for "[]" reference
if ($key == '') $key = count($target);
// Create the key in the parent array if not there yet
if (!isset($target[$key])) $target[$key] = array();
// Move pointer one level down the hierarchy
$target = &$target[$key];
}
// Write the value at the pointer location
$target = $value;
}
return $output;
}
You would call it like this:
$output = convert_formdata($input);
See it run on eval.in for the given input. The output is:
array (
'app_id' =>
array (
0 => 2,
1 => 3,
),
'fieldval' =>
array (
2 =>
array (
2 =>
array (
0 => 1,
),
),
3 =>
array (
3 =>
array (
0 => 200,
1 => 'day',
),
),
),
'title' => 'new plan,',
'feature_plan' => 3,
'plan_type' => 'free',
'price' => NULL,
'sell_type' => 'us',
)
Related
Assuming I have an array and recursively want to do some task. I have spent one day for this unable to solve this. May be my logic is not good.
Any help on how to do such thing in an efficient way would save my days.
I have an array with n level deep, looking like:
Array
(
[0] => Array
(
[name] => user1
[email] => user1#demo.com
[depth] => 1
)
[1] => Array
(
[name] => user2
[email] => user2#demo.com
[depth] => 1
[0] => Array
(
[0] => Array
(
[name] => user2.1
[email] => user2.1#demo.com
[depth] => 2
)
)
)
[2] => Array
(
[name] => user3
[email] => user3#demo.com
[depth] => 1
[0] => Array
(
[0] => Array
(
[name] => user3.1
[email] => user3.1#demo.com
[depth] => 2
[0] => Array
(
[0] => Array
(
[name] => user3.1.1
[email] => user3.1.1#demo.com
[depth] => 3
)
)
)
[1] => Array
(
[name] => user3.2
[email] => user3.2#demo.com
[depth] => 2
)
)
)
)
I want to change above array in exactly this format:
array(
0 => array(
'name' => 'user1',
),
1 => array(
'name' => 'user2',
'children' => array(
0 => array(
'name' => 'user2.1',
) ,
) ,
) ,
2 => array(
'name' => 'user3',
'children' => array(
0 => array(
'name' => 'user3.1',
'children' => array(
0 => array(
'name' => 'user3.1.1',
) ,
) ,
) ,
1 => array(
'name' => '3.2',
)
) ,
) ,
)
Edited:
I am using this code and working fine if i want to show data in tree format but unable to push data into array as i want.
function displayArrayRecursively($arr, $indent='') {
if ($arr) {
foreach ($arr as $key => $value) {
if (is_array($value)) {
displayArrayRecursively($value, $indent . '-->');
} else {
if ($key == 'name')
{
echo "$indent $value <br>";
}
else {
continue;
}
}
}
}
}
displayArrayRecursively($userListArray);
Can anyone suggest me how to do this?
Thank you
This function will do what you want:
function process_nodes($nodes) {
$new = array();
foreach ($nodes as $node) {
$new[] = array('name' => $node['name']);
if (isset($node[0])) {
$new[count($new)-1]['children'] = process_nodes($node[0]);
}
}
return $new;
}
print_r(process_nodes($data));
Output:
Array
(
[0] => Array
(
[name] => user1
)
[1] => Array
(
[name] => user2
[children] => Array
(
[0] => Array
(
[name] => user2.1
)
)
)
[2] => Array
(
[name] => user3
[children] => Array
(
[0] => Array
(
[name] => user3.1
[children] => Array
(
[0] => Array
(
[name] => user3.1.1
)
)
)
[1] => Array
(
[name] => user3.2
)
)
)
)
I want to change order of following array to 2nd array's values.
Array
(
[2] => Array
(
[title] => Photometric Interpretation
[name] => photometric_interpretation
)
[3] => Array
(
[title] => Make
[name] => make
)
[4] => Array
(
[title] => Model
[name] => model
)
[5] => Array
(
[title] => Strip Offsets
[name] => strip_offsets
)
[6] => Array
(
[title] => Samples Per Pixel
[name] => samples_per_pixel
)
[7] => Array
(
[title] => Rows Per Strip
[name] => rows_per_strip
)
)
I want to change order of above to following array's values.
Array
(
[0] => 3
[1] => 4
[2] => 7
[3] => 6
[4] => 5
[5] => 2
)
What I have tried
$index = array_flip(['3,4,7,6,5,2']);
$assigned_fields = array_merge($fisrt_array, $index);
My desired output is
Array
(
[3] => Array
(
[title] => Make
[name] => make
)
[4] => Array
(
[title] => Model
[name] => model
)
[7] => Array
(
[title] => Rows Per Strip
[name] => rows_per_strip
)
[6] => Array
(
[title] => Samples Per Pixel
[name] => samples_per_pixel
)
[5] => Array
(
[title] => Strip Offsets
[name] => strip_offsets
)
[2] => Array
(
[title] => Photometric Interpretation
[name] => photometric_interpretation
)
)
This should work fine.
$a = ['2' => ['title' => 'Photometric Interpretation',
'name' => 'photometric_interpretation'],
'3' => ['title' => 'Make',
'name' => 'make']];
$b = Array
(
0 => 3,
1 => 2
);
$c = [];
foreach($b as $s) {
$c[$s] = $a[$s];
}
print_r($c);
You need to use array_replace instead of array_merge.
$assigned_fields = array_replace(array_flip($index), $fisrt_array);
I'm have really lame question about PHP arrays, I'm trying to get all values of form[extras][], in this case they are 1,2,3,4, here is the output of print_r($array):
Array
(
[0] => Array
(
[name] => form[pickupDate][day]
[value] => 1
)
[1] => Array
(
[name] => form[pickupDate][month]
[value] => 10
)
[2] => Array
(
[name] => form[pickupTime][hour]
[value] => 0
)
[3] => Array
(
[name] => form[returnDate][day]
[value] => 1
)
[4] => Array
(
[name] => form[returnDate][month]
[value] => 1
)
[5] => Array
(
[name] => form[returnTime][hour]
[value] => 0
)
[6] => Array
(
[name] => form[car]
[value] => 1
)
[7] => Array
(
[name] => form[pickupAddress]
[value] =>
)
[8] => Array
(
[name] => form[agency]
[value] => 1
)
[9] => Array
(
[name] => form[extras][]
[value] => 1
)
[10] => Array
(
[name] => form[extras][]
[value] => 2
)
[11] => Array
(
[name] => form[extras][]
[value] => 3
)
[12] => Array
(
[name] => form[extras][]
[value] => 4
)
[13] => Array
(
[name] => form[specialPrice]
[value] =>
)
)
You can play with my data, by using this JSON string after converting it to PHP array like this:
$request = '[{"name":"form[pickupDate][day]","value":"1"},{"name":"form[pickupDate][month]","value":"8"},{"name":"form[pickupTime][hour]","value":"0"},{"name":"form[returnDate][day]","value":"1"},{"name":"form[returnDate][month]","value":"1"},{"name":"form[returnTime][hour]","value":"0"},{"name":"form[pickupAddress]","value":""},{"name":"form[agency]","value":"1"},{"name":"form[extras][]","value":"1"},{"name":"form[extras][]","value":"2"},{"name":"form[extras][]","value":"3"},{"name":"form[extras][]","value":"4"},{"name":"form[specialPrice]","value":""}]';
$array = json_decode($request,true);
I already tried with this, but it's resulting string(1) "4":
$result = array_column($array, null, 'name')['form[extras][]']['value'];
var_dump($result);
You have a multidimensional array, this is an array composed by several arrays.
array->[0]-[1]-[2]...
| |
| [name]-[value]
|
array-> [name]-[value]
That's why you have to go through each element, for instance with this function (you have one already defined in PHP > 5.5.0):
function array_column($array, $value)
{
$result = array();
foreach($array as $element)
{
$result[] = $element[$value];
}
return $result[];
}
Edit:
for getting the values of a subset of specific arrays based upon their name:
function getValuesOfTheArraysForExtras($array)
{
$result = array();
foreach($array as $element)
{
if($element['name']=='form[extras][]')
$result[] = $element['value'];
}
return $result;
}
I have this array $all_zones that comes sometimes with missing keys and values and I would like to fill the array with empty values for the messing keys, here's the array:
Array
(
[0] => Array
(
[id_zone] => 1
[name] => Europe
[price] => Array
(
[0] => 3.00
[1] => 6.00
)
[id_delivery] => Array
(
[0] => 1
[1] => 2
)
)
[1] => Array
(
[id_zone] => 3
[name] => Asia
)
[2] => Array
(
[id_zone] => 4
[name] => Africa
[price] => Array
(
[0] => 3.00
[1] => 6.00
)
[id_delivery] => Array
(
[0] => 3
[1] => 4
)
)
[3] => Array
(
[id_zone] => 5
[name] => Oceania
)
)
The thing is the $all_zones[$key]['price'] depend on how many ranges there's for each Zone, inthis case $range_count = count($all_ranges); will display 2, so I'd like to fill the missing keys for 2 times : Here's the output:
Array
(
[0] => Array
(
[id_zone] => 1
[name] => Europe
[price] => Array
(
[0] => 3.00
[1] => 6.00
)
[id_delivery] => Array
(
[0] => 1
[1] => 2
)
)
[1] => Array
(
[id_zone] => 3
[name] => Asia
[price] => Array
(
[0] =>
[1] =>
)
[id_delivery] => Array
(
[0] =>
[1] =>
)
)
[2] => Array
(
[id_zone] => 4
[name] => Africa
[price] => Array
(
[0] => 3.00
[1] => 6.00
)
[id_delivery] => Array
(
[0] => 3
[1] => 4
)
)
[3] => Array
(
[id_zone] => 5
[name] => Oceania
[price] => Array
(
[0] =>
[1] =>
)
[id_delivery] => Array
(
[0] =>
[1] =>
)
)
)
Here's what I've tried so far and didn't succeed:
$range_count = count($all_ranges);
$i=0;
foreach ($all_zones as $key => $value) {
if(isset($value['id_zone']) && isset($value['name']) && (!isset($value['price']) || !isset($value['id_delivery']))){
if($range_count>$i){
$disabled[]=$key;
$all_zones[$key]['price'][] = '';
$all_zones[$key]['id_delivery'][] = '';
}
$i++;
}
}
Any help with this? Much appreciated.
try this
$range_count = count($all_ranges);
foreach ($all_zones as $key => $value) {
if(isset($value['id_zone']) && isset($value['name']) && (!isset($value['price']) || !isset($value['id_delivery']))){
$disabled[]=$key;
if((!isset($value['price']))
{
for($i=0; $i<$range_count<$i++)
{
$all_zones[$key]['id_delivery'][] = '';
}
}
if((!isset($value['id_delivery']))
{
for($i=0; $i<$range_count<$i++)
{
$all_zones[$key]['id_delivery'][] = '';
}
}
}
}
You might have operator precedence problem.
( (!isset($value['price']) || !isset($value['id_delivery'])) )
The way to do this is to loop through the array, with an array_merge() on each array within the parent array to set your 'defaults'.
$zone_template = array(
'id_zone' => '',
'name' => '',
'price' => array(
0 => '',
1 => ''
),
'id_delivery' = array(
0 => '',
1 => ''
)
);
foreach ($all_zones as $zone) {
array_merge($zone_template, $zone);
}
Can also be done with array_walk()
I am attempting to add another child array to the last child array.
Here is a sample of my array,
Array
(
[Auto-Trail] => Array
(
[name] => Auto-Trail
[children] => Array
(
[2001] => Array
(
[name] => 2001
[children] => Array
(
[Tracker] => Array
(
[name] => Tracker
[children] => Array
(
[CK] => Array
(
[name] => CK
[children] => Array
(
[Fiat] => Array
(
[name] => Fiat
)
)
)
[EK] => Array
(
[name] => EK
[children] => Array
(
[Fiat] => Array
(
[name] => Fiat
)
)
)
)
)
[Cheyenne] => Array
(
[name] => Cheyenne
[children] => Array
(
[630S] => Array
(
[name] => 630S
[children] => Array
(
[Fiat] => Array
(
[name] => Fiat
)
[Merc] => Array
(
[name] => Merc
[children] => Array
(
[313] => Array
(
[name] => 313
)
[316] => Array
(
[name] => 316
)
)
)
)
)
[630] => Array
(
[name] => 630
[children] => Array
(
[Fiat] => Array
(
[name] => Fiat
)
[Merc] => Array
(
[name] => Merc
[children] => Array
(
[313] => Array
(
[name] => 313
)
[316] => Array
(
[name] => 316
)
)
)
)
)
[634] => Array
(
[name] => 634
[children] => Array
(
[Fiat] => Array
(
[name] => Fiat
)
[Merc] => Array
(
[name] => Merc
[children] => Array
(
[313] => Array
(
[name] => 313
)
[316] => Array
(
[name] => 316
)
)
)
)
)
[634U] => Array
(
[name] => 634U
[children] => Array
(
[Fiat] => Array
(
[name] => Fiat
)
[Merc] => Array
(
[name] => Merc
[children] => Array
(
[313] => Array
(
[name] => 313
)
[316] => Array
(
[name] => 316
)
)
)
)
)
This is the array I wish to add to every last element,
For example, I need to add the following to [CK][children][Fiat][children] (and every last children's children) - this array will be static, every last child will use the same array.
[BOF] => Array
(
[name] => Furniture
)
[CHA] => Array
(
[name] => Chassis
)
So ... ][CK][children][Fiat] would become the following,
[CK] => Array
(
[name] => CK
[children] => Array
(
[Fiat] => Array
(
[name] => Fiat
[children] => Array
(
[BOF] => Array
(
[name] => Furniture
)
[CHA] => Array
(
[name] => Chassis
)
)
)
)
)
Please note, Not every last child is the same indentation level, it could be 10 more children indentations. It must use the last array within the array of each array.
Apologies if it's hard to understand, I'm struggling to word exactly the way I need it to work.
Thank you for your time and effort.
check out this solution (quick'n'dirty):
function injectArray(array $target, array $inject, $depth = 0){
$hasChildrenKey = array_key_exists('children', $target);
$hasChildren = $hasChildrenKey && !empty($target['children']);
if($depth % 2 == 0){
foreach($target as $k => $v)
if(is_array($v))
$target[$k] = injectArray($v, $inject, $depth+1);
}
elseif(!$hasChildren){
if(!$hasChildrenKey)
$target['children'] = array();
$target['children'] = array_merge_recursive($target['children'], $inject);
}
else if($hasChildrenKey){
$target['children'] = injectArray($target['children'], $inject, $depth+1);
}
return $target;
};
$testArray = array(
'Auto-Trail' => array('name' => 'asdf',
'children' => array(
'2001' => array('name' => '2001',
'children' => array(
'Tracker' => array('name' => 'Tracker',
'children' => array(
'CK' => array('name' => 'CK',
'children' => array(
'Fiat' => array('name' => 'Fiat')
)
)
)
)
)
)
)
)
);
$testInjectArray = array('BOF' => array('name' => 'Furniture'), 'CHA' => array('name' => 'Chassis'));
$result = injectArray($testArray, $testInjectArray);
print_r($result);