Need help on PHP json_decode() [duplicate] - php

This question already has answers here:
Access array returned by a function in php
(5 answers)
How to extract and access data from JSON with PHP?
(1 answer)
How can I access an array/object?
(6 answers)
Closed 5 years ago.
Need help and advice on json_decode() in PHP. I am working on a CURL operation for an API call and the return result is a JSON. What is the syntax that I need to access the field pgid?
CURL Operation
$output = curl_exec($ch);
$rslt = json_decode($output, true);
var_dump($rslt);
The var_dump() output
array (size=1)
'protection-groups' => array (size=3)
0 => array (size=2)
'ppsDropped' => float 0
'pgid' => int 13
1 => array (size=2)
'ppsDropped' => float 7.9957930115316
'pgid' => int 18
2 => array (size=2)
'ppsDropped' => float 5302.7606884163
'pgid' => int 19

Try like this :
$output = curl_exec($ch);
$rslt = json_decode($output, true);
$idsArray = array();
foreach($rslt['protection-groups'] as $key => $value){
$pgId = $value['pgid'];
echo $pgId;
//Or in case you need all the PGID's in array then :
array_push($idsArray,$value['pgid']);
}
print_r($idsArray);
exit();

try below code that will loop through and get all pgid
foreach($rslt['protection-groups'] as $group)
{
echo $group['pgid'];
}

Related

how to convert array in php [duplicate]

This question already has answers here:
Convert multidimensional array into single array [duplicate]
(24 answers)
Closed 5 years ago.
Hi i got this array from my database.
array (size=4)
0 =>
array (size=1)
0 =>
array (size=1)
'email_1' => string 'denise#aaa.com' (length=18)
1 =>
array (size=1)
0 =>
array (size=1)
'email_1' => string 'denise#aaa.com' (length=18)
And i need to do get like this
array (size=4)
0 =>
array (size=1)
email_1' => string 'denise#aaa.com' (length=18)
1 =>
array (size=1)
'email_1' => string 'denise#aaa.com' (length=18)
I tried with array_merge and all. But no idea how to archive this?
Do it like below:-
$final_array = array();
foreach($original_array as $key=>$val){
$final_array[$key][] = $val[0]['email_1'];
}
print_r($final_array);
Output:-https://eval.in/848213
In php it is possible to do like this
foreach ($yourArray as $arr){
$result[] = $arr[0];
}
You can get also your desired output like this:
$result = array_map('array_collapse',$yourArray);
For this just assign like this to its first element.
foreach($yourArray as $array){
$array = $array[0];
}

How can I convert JSON Array to array in PHP? [duplicate]

This question already has answers here:
How to convert JSON string to array
(17 answers)
How to extract and access data from JSON with PHP?
(1 answer)
Closed 5 years ago.
{
"receiver_uid":[
"58a43a3e3fbbf3.61108490",
"58a43be07a3bc3.90311110",
"58da53ab5ce8d6.84754819"
]
}
This is the value I would like to convert to array. I know it's quite a simple question, but please can anyone help me?
Use json_decode
json_decode($your_jsonString,true)
You should use json_decode.
Try this:
$data = json_decode($your_json_string, TRUE);
The second parameter will make decoded json string into an associative arrays.
Example :-
$data = '{"receiver_uid":["58a43a3e3fbbf3.61108490","58a43be07a3bc3.90311110","58da53ab5ce8d6.84754819"]}';
$array = json_decode($data, true);
echo "<pre>";
print_r($array);
Output would be like
Array
(
[receiver_uid] => Array
(
[0] => 58a43a3e3fbbf3.61108490
[1] => 58a43be07a3bc3.90311110
[2] => 58da53ab5ce8d6.84754819
)
)

Converting multidimentional array into single array and removing the keys [duplicate]

This question already has answers here:
Is there a function to extract a 'column' from an array in PHP?
(15 answers)
Closed 5 months ago.
array (size=1551884)
0 =>
array (size=1)
'entity_id' => string '131813' (length=6)
1 =>
array (size=1)
'entity_id' => string '213808' (length=6)
2 =>
array (size=1)
'entity_id' => string '712885' (length=6)
is it possible to convert it to single array without the key 'entity_id' without a loop?
array
0 =>
131813
1 =>
213808
2 =>
712885
I have tried this one :
call_user_func_array('array_merge', $array)
but somehow is only returning 1 element
UPDATE:
here are the benchmark results from the given answers to this question:
php version > 5.6
array_column: 0.20802903175354
foreach: 0.46231913566589
array_map: 1.021989107132
php version > 7
array_column: 0.079965829849243
foreach: 0.15323305130005
array_map: 0.28970503807068
This is also possible with array_column.
$result = array_column($your_array, 'entity_id');
You can do this very easily with array_map like this:
$result = array_map(function($value) {
return $value['entity_id'];
}, $originalArray);
Working example: https://3v4l.org/JOEMI
Of course you could also do it with a foreach loop:
$result = [];
foreach($originalArray AS $entity) {
$result[] = $entity['entity_id'];
}
Working example: https://3v4l.org/9J5XH
I prefer the first option personally.
Update: the accepted answer is clearly the best way. Do that! Leaving this here for comparison.

How to convert array into JSON object in php [duplicate]

This question already has answers here:
Converting MySQL result array to JSON [duplicate]
(2 answers)
Closed 6 years ago.
I want to convert my array value:
Array ( [page_1] => fifth [page_2] => first [page_3] => fourth [page_4] => third )
Into JSON format is given below
{s:6:"page_1";s:5:"third";s:6:"page_2";s:5:"first";s:6:"page_3";s:6:"fourth";s:6:"page_4";s:5:"fifth";}
Can anyone please help me
You want to serialize you array.
You need to use serialize()
<?php
$a = array (
'page_1' => 'fifth',
'page_2' => 'first',
'page_3' => 'fourth',
'page_4' => 'third');
echo serialize($a);
// Outputs: a:4:{s:6:"page_1";s:5:"fifth";s:6:"page_2";s:5:"first";s:6:"page_3";s:6:"fourth";s:6:"page_4";s:5:"third";}
?>
$json = json_encode($array);
and otherwise
$array = json_decode($json, true);
When I insert the value in table it is inserting like
s:107:"a:4:{s:6:"page_1";s:5:"third";s:6:"page_2";s:5:"first";s:6:"page_3";s:6:"fourth";s:6:"page_4";s:5:"fifth";}";
don't know why cause when i display it that is right but in table it is inserting something like above

Converting string with keys and values to array [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
unserialize problem
I have a string in the form of:
a:16:{i:0;s:3:"696";i:1;s:3:"698";i:2;s:3:"690";}"
I am looking at turning this back into array, so that it will be along the lines of:
array(16) {
0 => 696,
1 => 698,
2 => 690
}
Any ideas how to do this?
Thanks
It looks like a serialized PHP string, try
$array = unserialize($value);
Manual: http://php.net/manual/en/function.unserialize.php
Update
The string contains a flaw, as it expects a array of 16 elements, but only 3 given.
Consider:
$a = array (
0 => '696',
1 => '698',
2 => '690'
);
$s = serialize($a);
will result in:
"a:3:{i:0;s:3:"696";i:1;s:3:"698";i:2;s:3:"690";}"
Use the unserialize() function.
$array = unserialize($serialized_string);

Categories