I have array like below,
Array
(
[14289] => Array
(
[0] => Karthikeyan
[1] => Mathi
[2] => RAvi
)
[14279] => Array
(
[0] => Karthikeyan
[1] => Mathi
[2] => RAvi
)
[14288] => Array
(
[0] => Karthikeyan
)
[14291] => Array
(
[0] => Mathi
)
[14284] => Array
(
[0] => RAvi
)
)
if I unset the index[1] value from [14289] this one. after that the array display like below,
Array
(
[14289] => Array
(
[0] => Karthikeyan
[2] => RAvi
)
[14279] => Array
(
[0] => Karthikeyan
[1] => Mathi
[2] => RAvi
)
[14288] => Array
(
[0] => Karthikeyan
)
[14291] => Array
(
[0] => Mathi
)
[14284] => Array
(
[0] => RAvi
)
)
But I need [14289] values like below,
Array
(
[14289] => Array
(
[0] => Karthikeyan
[1] => RAvi
)
........
the key value should be [1] not [2]. how can I do this?
after
unset $example_array[14289][1];
do
$example_array[14289]= array_values($example_array[14289]);
Use array_values on the 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
)
)
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));
I want to insert extra key before of array if key exists and array is not multi dimensional for example:
Array
(
[0] => Array
(
[_key_] => Array
(
[0] => Array
(
[pub-id-type] => pmid
[value] => 25588809
)
[1] => Array
(
[pub-id-type] => pmc
[value] => 4302133
)
[2] => Array
(
[pub-id-type] => publisher-id
[value] => 1008
)
[3] => Array
(
[pub-id-type] => doi
[value] => 10.1186/s12885-015-1008-4
)
[type_s] => article-id
[id] => 58a6eeedeab2f
)
)
I want:
Array
(
[0] => Array
(
[_key_] => Array
(
[0]=>array(
[0] => Array
(
[pub-id-type] => pmid
[value] => 25588809
)
[1] => Array
(
[pub-id-type] => pmc
[value] => 4302133
)
[2] => Array
(
[pub-id-type] => publisher-id
[value] => 1008
)
[3] => Array
(
[pub-id-type] => doi
[value] => 10.1186/s12885-015-1008-4
)
[type_s] => article-id
[id] => 58a6eeedeab2f
)
)
)
recursively util n depath of _Key_ is found.
The layout is not the best, but howerver ...
try this one:
if(is_array($arr[0][_key_]))
{
$tmp = $arr[0][_key_];
unset($arr[0][_key_]);
$arr[0][_key_][] = $tmp;
}
I need to merge a PHP array, this array has 2 arrays into it named "targetXX", I can have 2 or more. Each target have the same keys, for each key I have an array with 2 values a and b, a is always the same in both targets, but I need to merge both B values like this:
Array
(
[0] => Array
(
[target] => hitcount(stats.asdf1.requests, "1min")
[datapoints] => Array
(
[0] => Array
(
[0] => 1200
[1] => 1392282200
)
[1] => Array
(
[0] => 1400
[1] => 1392282260
)
[2] => Array
(
[0] => 600
[1] => 1392282320
)
[3] => Array
(
[0] => 200
[1] => 1392282380
)
[4] => Array
(
[0] => 400
[1] => 1392282440
)
[5] => Array
(
[0] => 600
[1] => 1392282500
)
)
)
[1] => Array
(
[target] => hitcount(stats.asdf.requests, "1min")
[datapoints] => Array
(
[0] => Array
(
[0] => 4321
[1] => 1392282200
)
[1] => Array
(
[0] => 76567
[1] => 1392282260
)
[2] => Array
(
[0] => 5556
[1] => 1392282320
)
[3] => Array
(
[0] => 7675
[1] => 1392282380
)
[4] => Array
(
[0] => 2344
[1] => 1392282440
)
[5] => Array
(
[0] => 0999
[1] => 1392282500
)
)
)
Result:
Array
(
[0] => Array
(
[target] => hitcount(stats.asdf1.requests, "1min")
[datapoints] => Array
(
[0] => Array
(
[0] => 1200
[1] => 1392282200
[2] => 4321
)
[1] => Array
(
[0] => 1400
[1] => 1392282260
[2] => 76567
)
[2] => Array
(
[0] => 600
[1] => 1392282320
[2] => 5556
)
[3] => Array
(
[0] => 200
[1] => 1392282380
[2] => 7675
)
[4] => Array
(
[0] => 400
[1] => 1392282440
[2] => 2344
)
[5] => Array
(
[0] => 600
[1] => 1392282500
[2] => 0999
)
)
)
Use array_merge() to achieve this:
$newArray = array();
foreach ($myArray['target2'] as $key => $innerArr1) {
$newArray['target'][$key] = array_merge(
$myArray['target1'][$key], /* 0th and 1st index */
array($innerArr1[1]) /* 2nd index */
);
}
print_r($newArray);
Output:
Array
(
[target] => Array
(
[0] => Array
(
[0] => 333333
[1] => 13
[2] => 99
)
[1] => Array
(
[0] => 444444
[1] => 15
[2] => 98
)
[2] => Array
(
[0] => 555555
[1] => 17
[2] => 97
)
)
)
Demo
The built-in function array_merge may do the work for you. You need to merge each subarrays in fact, as the array_merge_recursive function doesn't handle indexes.
$newArray = array();
foreach ($myArray['target2'] as $key => $arr) {
$newArray['target'][$key] = array_merge($myArray['target1'][$key], $arr[1]);
}
Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.
If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
If you have more than 2 keys to merge, you can loop on the algorithm multiple times.
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');