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;
}
Related
I'm trying to flat a multidimensional array to a given specific format.
I have a tree that is saved as a nested array, which is ok, but the function given to render the array in the UI expects only one array and, per each child, an independent array. Instead of the nested array, each option should be at the same level.
This is how my var_dump of my array:
(
[id] => 1
[name] => some data.
[emoji] => 🐕
[parent_id] =>
[children] => Array
(
[0] => Array
(
[id] => 2
[name] => Food
[emoji] => 🥩
[parent_id] => 1
[children] => Array
(
)
)
[1] => Array
(
[id] => 3
[name] => some other data
[emoji] => 😌
[parent_id] => 1
[children] => Array
(
[0] => Array
(
[id] => 4
[name] => Massages
[emoji] => 💆
[parent_id] => 3
[children] => Array
(
)
)
[1] => Array
(
[id] => 5
[name] => Games
[emoji] => 🎾
[parent_id] => 3
[children] => Array
(
)
)
)
)
)
)
)
And the expected result should be:
0] => Array
(
[id] => 1
[name] => Rusty Corp.
[emoji] => 🐕
[parent_id] =>
)
[1] => Array
(
[id] => 2
[name] => Food
[emoji] => 🥩
[parent_id] => 1
)
[2] => Array
(
[id] => 3
[name] => Canine Therapy
[emoji] => 😌
[parent_id] => 1
)
[3] => Array
(
[id] => 4
[name] => Massages
[emoji] => 💆
[parent_id] => 3
)
[4] => Array
(
[id] => 5
[name] => Games
[emoji] => 🎾
[parent_id] => 3
)
I tried different approaches like array_merge or custom flattening function but can't nail with the expected results, any suggestions?
Edit:
This is my flatten function:
private function flatten_array( array $array ) {
$return = array();
array_walk_recursive(
$array,
function( $a ) use ( &$return ) {
$return[] = $a;
}
);
return $return;
}
Here is a recursive function that will flatten the array, it will not take into account the null value of parent_id on the root element. Also the flattened array will start with the most nested elements at the start of the array and root element at the end.
function flatten_array($array, $flattened = []) {
$current = [];
foreach ($array as $key => $value) {
if (is_array($value))
$flattened = array_merge($flattened, flatten_array($value));
else
$current[$key] = $value;
}
$flattened[] = $current;
return array_filter($flattened);
}
This question already has answers here:
PHP array delete by value (not key)
(20 answers)
Closed 3 years ago.
Following is my array and i want to remove specific value from array.
Array
(
[0] => Array
(
[name] => categoryfilter
[value] => 127
)
[1] => Array
(
[name] => price_min
[value] => sd
)
[2] => Array
(
[name] => price_max
[value] => sdsd
)
[3] => Array
(
[name] => action
[value] => myfilter
)
[4] => Array
(
[name] => quantity
[value] => 1
)
[5] => Array
(
[name] => quantity
[value] => 1
)
[6] => Array
(
[name] => quantity
[value] => 1
)
[7] => Array
(
[name] => quantity
[value] => 0
)
[8] => Array
(
[name] => quantity
[value] => 0
)
[9] => Array
(
[name] => quantity
[value] => 0
)
[10] => Array
(
[name] => quantity
[value] => 1
)
);
I want to remove all quantity key items from array .
I have tried using following way but not remove display same thing.
if (($key = array_search('quantity', $_POST['product'])) !== false) {
unset($_POST['product'][$key]);
}
echo "<pre>";print_r($_POST['product']);
Loop through the array.
foreach ($_POST['product'] as $k => $p) {
if ($p['name'] == 'quantity') {
unset($_POST['product'][$k];
}
}
Use array_filter with callback function
$f = array_filter($a, function($v){return $v['name'] != 'quantity';});
Working example : https://3v4l.org/DqoLj
I have a problem, I would like to merge arrays by value. Below is a entry example, the entry array have a 100 records
Array
(
[0] => Array
(
[id] => 1
[code] => dfrr5tv5t5vt5
[status] => online
)
[1] => Array
(
[id] => 2
[code] => e32e3e2e2323e23e
[status] => online
)
[2] => Array
(
[id] => 1
[desc] => Some_description
)
[3] => Array
(
[id] => 2
[desc] => Some_description_2
)
....
)
I would like to get the following result through merge array by [id]
Array
(
[0] => Array
(
[id] => 1
[code] => dfrr5tv5t5vt5
[status] => online
[desc] => Some_description
)
[1] => Array
(
[id] => 2
[code] => e32e3e2e2323e23e
[status] => online
[desc] => Some_description_2
)
....
)
Use assocative arrays. Use $row["id"] as associative index.
$result = [];
foreach ($arr as $row) {
$result[$row["id"]] = isset($result[$row["id"]]) ? array_merge($result[$row["id"]], $row) : $row;
}
$result = array_values($result); // optional, this removes associative keys
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 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',
)