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;
}
Related
Could you please help me? I need to know how to access the values of this response from an API call using PHP.
Array ( [queryIds] => Array ( [0] => lHLLapn7EH ) [timedOutRealtimeData] => [realtimeDataMissing] => [cacheFreshness] => FRESH [prunedResult] => [novaCost] => 0 [novaRequestDuration] => 19 [wasCached] => 1 [minSampleRate] => 1 [timeComputed] => 1558794556710 [novaRuntime] => 199 [hitChunkGroupByLimit] => [throttleTime] => 0 [data] => Array ( [series] => Array ( [0] => Array ( [0] => Array ( [setId] => [value] => 843 ) ) [1] => Array ( [0] => Array ( [setId] => [value] => 22 ) ) ) [seriesLabels] => Array ( [0] => Array ( [0] => 0 [1] => C5241; GB ) [1] => Array ( [0] => 0 [1] => C9999; GB ) ) [seriesMeta] => Array ( [0] => Array ( [eventGroupBys] => Array ( [0] => C5241 [1] => GB ) [segmentIndex] => 0 [eventIndex] => 0 [eventGroupBy] => C5241; GB ) [1] => Array ( [eventGroupBys] => Array ( [0] => C9999 [1] => GB ) [segmentIndex] => 0 [eventIndex] => 0 [eventGroupBy] => C9999; GB ) ) [seriesCollapsed] => Array ( [0] => Array ( [0] => Array ( [setId] => [value] => 843 ) ) [1] => Array ( [0] => Array ( [setId] => [value] => 22 ) ) ) [xValues] => Array ( [0] => 2019-05-25 ) ) [partialMergedAndNewUserInformation] => [transformationIds] => Array ( [0] => 2696 ) [backend] => novaV2 )
You can access them using
Array ( [0] => C9999 [1] => GB ) -> $arr['data']['seriesMeta'][0]['eventGroupBys']
Array ( [setId] => [value] => 20) -> $arr['data']['seriesMeta'][0][0]
I have this array of userids. I want to combine the child userid arrays into one array within the parent arrays.
[0] => Array
(
[0] => Array
(
[userid] => 1610
)
[1] => Array
(
[userid] => 1614
)
[2] => Array
(
[userid] => 1616
)
)
[1] => Array
(
[0] => Array
(
[userid] => 1610
)
[1] => Array
(
[userid] => 1614
)
[2] => Array
(
[userid] => 1616
)
[3] => Array
(
[userid] => 1618
)
)
My desired result would look like...
[0] => Array
(
[userids] => Array
(
[0] => 1610
[1] => 1614
[2] => 1616
)
)
[1] => Array
(
[userids] => Array
(
[0] => 1610
[1] => 1614
[2] => 1616
[3] => 1618
)
)
Loop the array and use array_column to flatten the subarrays.
foreach($arr as $sub){
$res[][key($sub[0]) . "s"] = array_column($sub, 'userid');
}
var_dump($res);
https://3v4l.org/A68VA
The array is received from the facebook api and i am not able to extract the likes array from the array,Please help me
[data] => Array (
[0] => Array (
[message] => Hello
[id] => 729659027165160_729651713832558
[likes] => Array (
[data] => Array (
[0] => Array (
[id] => 729659027165160
)
)
[paging] => Array (
[cursors] => Array (
[after] => NzI5NjU5MDI3MTY1MTYw
[before] => NzI5NjU5MDI3MTY1MTYw
)
)
)
)
[1] => Array (
[id] => 729659027165160_718306454967084
[likes] => Array (
[data] => Array (
[0] => Array (
[id] => 1719747118259908
)
)
[paging] => Array (
[cursors] => Array (
[after] => MTcxOTc0NzExODI1OTkwOA==
[before] => MTcxOTc0NzExODI1OTkwOA==
)
)
)
)
[2] => Array (
[id] => 729659027165160_541135166017548
[likes] => Array (
[data] => Array (
[0] => Array (
[id] => 1162428970453842
)
)
[paging] => Array (
[cursors] => Array (
[after] => MTE2MjQyODk3MDQ1Mzg0Mg==
[before] => MTE2MjQyODk3MDQ1Mzg0Mg==
)
)
)
)
[3] => Array (
[message] => Panipaata leni prathivaadu philosophy cheppevade.... Wish Facebook introduce an unlike button soon!!!!
[id] => 729659027165160_520677651396633
[likes] => Array (
[data] => Array (
[0] => Array (
[id] => 1162428970453842
)
[1] => Array (
[id] => 806391372817118
)
[2] => Array (
[id] => 928633297192567
)
[3] => Array (
[id] => 824812004311172
)
[4] => Array (
[id] => 10207344532684729
)
[5] => Array (
[id] => 1188171664544003
)
)
[paging] => Array (
[cursors] => Array (
[after] => MTE4ODE3MTY2NDU0NDAwMw==
[before] => MTE2MjQyODk3MDQ1Mzg0Mg==
)
)
)
)
[4] => Array (
[id] => 729659027165160_110578795739856
[likes] => Array (
[data] => Array (
[0] => Array (
[id] => 1162428970453842
)
)
[paging] => Array (
[cursors] => Array (
[after] => MTE2MjQyODk3MDQ1Mzg0Mg==
[before] => MTE2MjQyO
)
)
)
)
)
I am able to extract the id from the above array,but unable to extract the count of likes and message.
Try this code, it iterates in your array and stores all ids and stores the message, the likes Array and the number of likes only if they exist (Supposing that your array is named $myarray):
$result = array();
foreach($myarray['data'] as $data){
$item = array();
$item['id'] = $data['id'];
if( isset($data['message']) || isset($data['likes']) ){
if(isset($data['message'])) $item['message'] = $data['message'];
if(isset($data['likes'])) {
$item['likes'] = array();
foreach($data['likes']['data'] as $like){
$item['likes'][] = $like['id'];
}
$item['countlikes'] = count( $data['likes']['data'] );
}
}
$result[] = $item;
}
print_r($result);
With your example Array the result will be:
Array
(
[0] => Array
(
[id] => 729659027165160_729651713832558
[message] => Hello
[likes] => Array
(
[0] => 729659027165160
)
[countlikes] => 1
)
[1] => Array
(
[id] => 729659027165160_718306454967084
[likes] => Array
(
[0] => 1719747118259908
)
[countlikes] => 1
)
[2] => Array
(
[id] => 729659027165160_541135166017548
[likes] => Array
(
[0] => 1162428970453842
)
[countlikes] => 1
)
[3] => Array
(
[id] => 729659027165160_520677651396633
[message] => Panipaata leni prathivaadu philosophy cheppevade.... Wish Facebook introduce an unlike button soon!!!!
[likes] => Array
(
[0] => 1162428970453842
[1] => 806391372817118
[2] => 928633297192567
[3] => 824812004311172
[4] => 10207344532684729
[5] => 1188171664544003
)
[countlikes] => 6
)
[4] => Array
(
[id] => 729659027165160_110578795739856
[likes] => Array
(
[0] => 1162428970453842
)
[countlikes] => 1
)
)
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 am new to php. I need some help.
I had a array as
Array ( [_] => Array ( [0] => [1] => )
[123_] => Array ( [0] => 123 [1] => )
[1234_] => Array ( [0] => 1234 [1] => )
)
Array ( [_] => Array ( [0] => [1] => )
[12345_] => Array ( [0] => 12345 [1] => )
[1234_] => Array ( [0] => 1234 [1] => )
)
so..whats my problem is i want an array with all these keys and values as
Array ( [_] => Array ( [0] => [1] => )
[123_] => Array ( [0] => 123 [1] => )
[1234_] => Array ( [0] => 1234 [1] => )
[_] => Array ( [0] => [1] => )
[12345_] => Array ( [0] => 12345 [1] => )
[1234_] => Array ( [0] => 1234 [1] => )
)
there would be duplicate keys and values.. but I want all of them as a array.. any help plz..
That is not possible. A PHP array cannot have two identical keys.
As the others said, it's impossible to have a single array with duplicate keys. But you can build an array of array :
<?php
$arr1 = array( '_' => Array ( '0' => '', '1' => ''),
'123_' => Array ( '0' => 123, '1' => ''),
'1234_' => Array ( '0' => 1234, '1' => '')
);
$arr2 = array ( '_' => Array ( '0' => '', '1' => ''),
'12345_' => Array ( '0' => 12345, '1' => ''),
'1234_' => Array ( '0' => 1234, '1' => '')
);
$result = array();
foreach( $arr1 as $key => $val) {
$result[] = array('key'=>$key, 'value'=>$val);
}
foreach( $arr2 as $key => $val) {
$result[] = array('key'=>$key, 'value'=>$val);
}
print_r($result);
?>
Ouput:
Array
(
[0] => Array
(
[key] => _
[value] => Array
(
[0] =>
[1] =>
)
)
[1] => Array
(
[key] => 123_
[value] => Array
(
[0] => 123
[1] =>
)
)
[2] => Array
(
[key] => 1234_
[value] => Array
(
[0] => 1234
[1] =>
)
)
[3] => Array
(
[key] => _
[value] => Array
(
[0] =>
[1] =>
)
)
[4] => Array
(
[key] => 12345_
[value] => Array
(
[0] => 12345
[1] =>
)
)
[5] => Array
(
[key] => 1234_
[value] => Array
(
[0] => 1234
[1] =>
)
)
)
Have a look at PHP's array_merge()-function.