This is my generated array
i have tried array_merge_recrusive but not successful.
I am doing program it should generate this array
Array
(
[0] => Array
(
[competancy] => Array
(
[0] => 1
)
)
[1] => Array
(
[assistment] => Array
(
[0] => 0
)
)
[2] => Array
(
[competancy] => Array
(
[0] => 3
)
)
[3] => Array
(
[competancy] => Array
(
[0] => 4
)
)
[4] => Array
(
[assistment] => Array
(
[0] => 0
)
)
[5] => Array
(
[competancy] => Array
(
[0] => 6
)
)
required
[6] => Array
(
[competancy] => Array
(
[0] => 7
)
)
[7] => Array
(
[assistment] => Array
(
[0] => 0
)
)
)
I want following output array
I have tried lot of but not find this type of output
Array
(
[competancy] => Array
(
[0] => 1
)
[assistment] => Array
(
[0] => 0
)
[competancy] => Array
(
[0] => 3
[0] => 4
)
[assistment] => Array
(
[0] => 0
)
[competancy] => Array
(
[0] => 6
[0] => 7
)
[assistment] => Array
(
[0] => 0
)
)
Try to merge array with splat :
print_r(array_merge(...$array));
Related
My problem is that I want to sort array keys by ascending and I am googling last 2 days what I am getting the solution is uksort, ksort but it's not give the exact solution what am I want.I feel great full for helping to solve this issue. My output array is:-
Array
(
[2020-06-22,13:00] => Array
(
[0] => 1663
[1] => 1664
)
[2020-06-22,14:00] => Array
(
[0] => 1665
[1] => 1666
)
[2020-06-24,13:00] => Array
(
[0] => 1715
)
[2020-06-24,8:30] => Array
(
[0] => 1714
)
[2020-06-23,13:00] => Array
(
[0] => 1724
)
[2020-06-23,8:30] => Array
(
[0] => 1727
)
)
and we need array is in:-
Array
(
[2020-06-22,13:00] => Array
(
[0] => 1663
[1] => 1664
)
[2020-06-22,14:00] => Array
(
[0] => 1665
[1] => 1666
)
[2020-06-23,8:30] => Array
(
[0] => 1727
)
[2020-06-23,13:00] => Array
(
[0] => 1724
)
[2020-06-24,8:30] => Array
(
[0] => 1714
)
[2020-06-24,13:00] => Array
(
[0] => 1715
)
)
I have an array
Array
(
[0] => Array
(
[0] => 96
[1] => ML based
[7] => Open
)
)
Array
(
[0] => Array
(
[0] => 97
[1] => Application
[7] => Open
)
)
Array
(
[0] => Array
(
[0] => 98
)
)
Array
(
[0] => Array
(
[0] => 99
)
)
I want to remove
Array
(
[0] => Array
(
[0] => 98
)
)
Array
(
[0] => Array
(
[0] => 99
)
)
from this array
I tried:
$data = array_map('array_filter', $rowData);
unset($data[0][0]);
Expected output:
Array
(
[0] => Array
(
[0] => 96
[1] => ML based )
[7] => Open
)
)
Array
(
[0] => Array
(
[0] => 97
[1] => Application
[7] => Open
)
)
any help would be Appreciated .
array_filter() will work. Try -
array_filter($array, function ($a) {
return count($a[0]) == 3; // return array with 3 elements only
});
Working code
This question already has answers here:
Map/Merge data from a flat array into the rows of a 2d array [duplicate]
(5 answers)
Closed 5 months ago.
I have two arrays. One is a multidimensional array and the other is structured normally as seen below.
Array (
[0] = Array
(
[0] => Array
(
[james] => 1
[kevin] => 2
)
[1] => Array
(
[joe] => 1
[jim] => 2
)
)
[1] = Array
(
[0] => Array
(
[jill] => 1
[john] => 2
)
[1] => Array
(
[janet] => 1
[clarence] => 2
)
)
)
and the second array
Array
(
[0] => Array
(
[total_stuff] => 75210
)
[1] => Array
(
[total_stuff] => 95640
)
)
How would I append the first value of the second array to the end of the first inner array within the multidimensional array so it would look like the array that follows? I need to preserve the values of the second array but not the keys.
Array (
[0] = Array
(
[0] => Array
(
[james] => 1
[kevin] => 2
)
[1] => Array
(
[joe] => 1
[jim] => 2
)
[2] => Array
(
[total_stuff] => 75210
)
)
[1] = Array
(
[0] => Array
(
[jill] => 1
[john] => 2
)
[1] => Array
(
[janet] => 1
[clarence] => 2
)
[2] => Array
(
[total_stuff] => 95640
)
)
)
You can do it using array map and array merge
<?php
$a1=array (
array
(
array
(
"james" => 1,
"kevin" => 2
),
array
(
"joe" => 1,
"jim" => 2
)
),
array
(
array
(
"jill" => 1,
"john" => 2
),
array
(
"janet" => 1,
"clarence" => 2
)
)
);
$a2=array
(
array
(
"total_stuff" => 75210
),
array
(
"total_stuff" => 95640
)
);
//merge each index with corresponding index of second array to form new array as you desired
$new = array_map(function ($a,$k)use($a2) { return array_merge($a,array($a2[$k])); }, $a1,array_keys($a1));
echo "<pre>";
print_r($new);
?>
output
Array
(
[0] => Array
(
[0] => Array
(
[james] => 1
[kevin] => 2
)
[1] => Array
(
[joe] => 1
[jim] => 2
)
[2] => Array
(
[total_stuff] => 75210
)
)
[1] => Array
(
[0] => Array
(
[jill] => 1
[john] => 2
)
[1] => Array
(
[janet] => 1
[clarence] => 2
)
[2] => Array
(
[total_stuff] => 95640
)
)
)
working fiddle http://phpfiddle.org/main/code/ymf7-69si
I have to post a form element inventory like following structure
[inventory] => Array
(
[0] => Array
(
[inventory_id] => Array
(
[0] => 1
)
[inventory_name] => Array
(
[0] => Bed 90*200
)
[inventory_photo] => Array
(
[0] => 1_bed_90x200.jpg
)
)
[1] => Array
(
[inventory_id] => Array
(
[0] => 15
)
[inventory_name] => Array
(
[0] => Bed 90*200
)
[inventory_photo] => Array
(
[0] => 1_bed_90x200.jpg
)
)
[2] => Array
(
[inventory_id] => Array
(
[0] => 15
)
[inventory_name] => Array
(
[0] => Bed 90*200
)
[inventory_photo] => Array
(
[0] => 1_bed_90x200.jpg
)
)
)
When I tried to assign this array to inventory in $client->setParameterPost(), I received POST values like this
[inventory] => Array
(
[0] => Array
(
[inventory_id] => Array
(
[0] => 1
)
)
[1] => Array
(
[inventory_name] => Array
(
[0] => Bed 90*200
)
)
[2] => Array
(
[inventory_photo] => Array
(
[0] => 1_bed_90x200.jpg
)
)
[3] => Array
(
[inventory_id] => Array
(
[0] => 15
)
)
[4] => Array
(
[inventory_name] => Array
(
[0] => Bed 90*200
)
)
[5] => Array
(
[inventory_photo] => Array
(
[0] => 1_bed_90x200.jpg
)
)
[6] => Array
(
[inventory_id] => Array
(
[0] => 15
)
)
[7] => Array
(
[inventory_name] => Array
(
[0] => Bed 90*200
)
)
[8] => Array
(
[inventory_photo] => Array
(
[0] => 1_bed_90x200.jpg
)
)
)
)
I have verified my array structure that is fine. I also checked in setParameter method in Client.php (Zend library), no issues. Just receiving this post. How can I achieve this?
Zend_Http_Client ignores integer keys of multi dimensional parameters. So in your case,
[inventory] => Array
(
[0] => Array
(
[inventory_id] => Array
(
[0] => 1
)
[inventory_name] => Array
(
[0] => Bed 90*200
)
[inventory_photo] => Array
(
[0] => 1_bed_90x200.jpg
)
)
will be translated to
inventory[][inventory_id][] => 1
inventory[][inventory_name][] => Bed 90*200
inventory[][inventory_photo][] => 1_bed_90x200.jpg
The solution to this problem is
either to use a class extended from Zend_Http_Client and override its method _flattenParametersArray().
or convert the params arrays to strings yourself, such as:
$client->setParameterPost('inventory[0][inventory_id][]', 1);
$client->setParameterPost('inventory[0][inventory_name][]', 'Bed 90*200');
$client->setParameterPost('inventory[0][inventory_photo][]', '1_bed_90x200.jpg');
I have array like below:
Array
(
[_edit_lock] => Array
(
[0] => 1309611144:1
)
[_edit_last] => Array
(
[0] => 1
)
[banner_url] => Array
(
[0] => http://apptivowp.apptivo.com/awp-content/10001/uploads/2011/07/gallery_banner.jpg
)
[_yoast_wpseo_focuskw] => Array
(
[0] =>
)
[_yoast_wpseo_title] => Array
(
[0] =>
)
)
How can I get the banner_url value?
As simple as:
$array['banner_url'][0]