PHP JSON Multi-dimensional array outputting only 1 result - php

I have been stumped on this for hours. I need to get the initial key of the array and the id. However I am only getting 1 result returned back.
Below is example code and also here is a link - https://3v4l.org/HdMtA
In short the expected output should be.
key 11111111 id val_somevalue5555
key 2222222 id val_somevalue25
I am only getting one result.
key 11111111 id val_somevalue5555
.
$json = '[{
"11111111": {
"id": "val_somevalue5555",
"customer": {
"32312": {
"name": "john doe"
}
}
},
"2222222": {
"id": "val_somevalue25",
"customer": {
"32312234": {
"name": "jane doe"
}
}
}
}]';
$jsonarr = json_decode($json, true);
$newarr = [];
foreach($jsonarr as $value)
{
$key = key($value);
$newarr[] = ['key' => $key, 'id' => $value[$key]['id']];
}
var_dump($newarr);
Any help would be appreciated not sure if its the format of the json or what.

You are iterating the wrong thing.
Using print_r() to view the contents of the decoded array shows us that the thing you want to iterate over is - in fact - wrapped in another array.
print_r($jsonarr) returns this:
Array
(
[0] => Array
(
[11111111] => Array
(
[id] => val_somevalue5555
[customer] => Array
(
[32312] => Array
(
[name] => jane doe
)
)
)
[2222222] => Array
(
[id] => val_somevalue25
[customer] => Array
(
[32312234] => Array
(
[name] => jane doe
)
)
)
)
)
So, what you have is a JSON object wrapped in JSON array with said object being the only item inside it.
You want to either:
a) Get rid of those [ and ] things at the beginning and the end of your JSON, or... (if you don't have control over that JSON)
b) Iterate the inner object (PHP represents it as associative array):
$jsonarr = json_decode($json, true);
$newarr = [];
foreach($jsonarr[0] as $key => $value) {
$newarr[] = ['key' => $key, 'id' => $value['id']];
}`
var_dump($newarr);
Behold: https://3v4l.org/qCvRd

Change your loop to use the key from the foreach loop itself: Like this
foreach($jsonarr as $key => $value)
{
$newarr[] = ['key' => $key, 'id' => $value[$key]['id']]; //this seems off but what do I know.
}
PHP uses a copy ( or reference?) of the array for Foreach, it can produce some unexpected things to happen. It does some spooky voodoo stuff some times, if you ever try editing the array structure while using reference in foreach such as foreach($var as $k => &$v ) look out. I swear one time it was like phantom items in the array ... lol

You need to change in you json structure and then make necessary changes which I mentioned below
$json = '{
"11111111": {
"id": "val_somevalue5555",
"customer": {
"32312": {
"name": "john doe"
}
}
},
"2222222": {
"id": "val_somevalue25",
"customer": {
"32312234": {
"name": "jane doe"
}
}
}
}';
$jsonarr = json_decode($json, true);
$newarr = [];
foreach($jsonarr as $key=> $value)
{
$newarr[] = ['key' => $key, 'id' => $value['id']];
}
var_dump($newarr);

Are you looking this o/p:
$json = '[{
"11111111": {
"id": "val_somevalue5555",
"customer": {
"32312": {
"name": "john doe"
}
}
},
"2222222": {
"id": "val_somevalue25",
"customer": {
"32312234": {
"name": "jane doe"
}
}
}
}]';
$jsonarr = json_decode($json, true);
$newarr = [];
foreach($jsonarr[0] as $key => $value)
{
//echo "<br />".$key = key($value);
$newarr[] = ['key' => $key, 'id' => $value['id']];
}
var_dump($newarr);
Output:
array(2) {
[0]=>
array(2) {
["key"]=>
int(11111111)
["id"]=>
string(17) "val_somevalue5555"
}
[1]=>
array(2) {
["key"]=>
int(2222222)
["id"]=>
string(15) "val_somevalue25"
}
}
Or you can change the json structure as ankit patel said and follow his code...

Add another foreach to loop the elements of [0] if you will have more than this.
foreach($jsonarr as $value)
{
foreach($value as $key => $val){
$newarr[] = ['key' => $key, 'id' => $val['id']];
}
}
Result
array(2) {
[0]=>
array(2) {
["key"]=>
int(11111111)
["id"]=>
string(17) "val_somevalue5555"
}
[1]=>
array(2) {
["key"]=>
int(2222222)
["id"]=>
string(15) "val_somevalue25"
}
}
Test here

Related

Convert an array to multidimensional array

I have an array that looks something like this:
array:2 [
"test1_test2_test3" => "result"
"category_slug" => "the_slug"
]
What I need to do is convert it to a multidimensional array that would look something like this:
array:2 [
"test1" => [
"test2" => [
"test3" => "result"
]
]
"category" => [
"slug" => "the_slug"
]
]
I know that I can explode on the key to get an array but am unsure of how to then go from this to end up with the final result.
EDIT The array looks like that initially because it's being pulled from request parameters: http://url.com?test1.test2.test3=result&category.slug=the_slug and Laravel auto converts it to an array.
A simple solution:
$result = [];
foreach ($array as $key => $value) {
foreach (array_reverse(explode('_', $key)) as $key_part) {
$value = [$key_part => $value];
}
$result += $value;
}
If you need to handle several keys with the same parts (such as test1_test2_test3 and test1_test2_test4), replace the last line with:
$result = array_merge_recursive($result, $value);
My approach would be to reverse the array, then loop through the keys and nest them.
The code below should do the trick.
$array = [
"test1_test2_test3" => "result",
"category_slug" => "the_slug"
];
$array = array_map(function ($key, $value) {
$keys = array_reverse(explode('_', $key));
while($key = array_shift($keys)) {
$value = [$key => $value];
}
return $value;
}, array_keys($array), $array);
$array = call_user_func_array('array_merge', $array);
var_dump($array);
/**
array(2) {
["test1"]=>
array(1) {
["test2"]=>
array(1) {
["test3"]=>
string(6) "result"
}
}
["category"]=>
array(1) {
["slug"]=>
string(8) "the_slug"
}
}
*/
One way to go:
$arr = array("test1_test2_test3" => "result", "category_slug" => "the_slug");
$res = array();
foreach($arr as $k=>$v) {
$t = explode("_", $k);
$new_arr = array();
$tmp = $v;
for($i=count($t)-1; $i > 0; $i--) {
$new_arr[$t[$i]] = $tmp;
$tmp = $new_arr;
$new_arr = array();
}
$res[$t[0]] = $tmp;
}
print_r($res);
Result:
Array
(
[test1] => Array
(
[test2] => Array
(
[test3] => result
)
)
[category] => Array
(
[slug] => the_slug
)
)
Looking through the Laravel documentation I found a helper array_set, which means in order to change the key to a multidimensional array I can change the key to use dot notation with str_replace and then use the helper to convert it over:
$array = [
"test1_test2_test3" => "result"
"category_slug" => "the_slug"
]
$new_array = []
foreach($array as $key => $value)
{
$key = str_replace('_', '.', $key);
array_set($new_array, $key, $value);
}
Result:
array:2 [
"test1" => [
"test2" => [
"test3" => "result"
]
]
"category" => [
"slug" => "the_slug"
]
]

PHP get index into new array

I have a json array like below. I need to get the index into a new array, how is this possible? Arrays are my weakness for some reason just cant grasp them. I can easily get id value, but cannot get the index (e.g 11111111). Any help would be appreciated.
Update please see the revised, my fault for not including the full multi dimensional array.
Below only outputs one result where I need all results.
<?php
$json = '[{
"11111111": {
"id": "val_somevalue5555",
"customer": {
"32312": {
"name": "jane doe"
}
}
},
"2222222": {
"id": "val_somevalue25",
"customer": {
"32312234": {
"name": "jane doe"
}
}
}
}]';
$jsonarr = json_decode($json, true);
$newarr = [];
foreach($jsonarr as $value)
{
$key = key($value);
$newarr[] = ['key' => $key, 'id' => $value[$key]['id']];
}
var_dump($newarr);
expected looped output
key 11111111
id val_somevalue5555
... looped.
You can create an array of the keys of an existing array using the array_keys() function
http://php.net/manual/en/function.array-keys.php
If you don't want the keys in a separate array, and instead just want to access them directly, when you are doing a 'foreach' loop of an array, you can choose to assign a variable to the current key by doing
foreach($jsonarr as $key => $value){...}
Because your original array is actually multidimensional (each $key has a $value that is also stored as an array of "id": "value") - this means taking one more step to get the value of key 'id':
foreach($jsonarr as $key => $value){
$newarray[] = ['key' => $key, 'id' => $value['id'];
}
you can use array_keys() or key() with a foreach loop for this(DEMO):
$newarr = [];
foreach($jsonarr as $value)
{
//$key = array_keys($value)[0];
$key = key($value);
$newarr[] = ['key' => $key, 'id' => $value[$key]['id']];
}
var_dump($newarr);
Output:
array(2) {
[0]=>
array(2) {
["key"]=>
int(11111111)
["id"]=>
string(17) "val_somevalue5555"
}
[1]=>
array(2) {
["key"]=>
int(2222222)
["id"]=>
string(15) "val_somevalue25"
}
}
Edit: With the updated json, you can use the following way, using 2 foreach loops (DEMO):
$newarr = [];
foreach($jsonarr as $json)
{
foreach($json as $key => $value)
{
$newarr[] = ['key' => $key, 'id' => $value['id']];
}
}
PHP supports a slightly different foreach syntax that extracts both the array key and the array value:
foreach ( $jsonarr as $key => $value ) {
$newarr[] = ['key' => $key, 'id' => $value];
}
Use this if you need the key ("11111111" and "2222222" in your example).
<?php
$json = '[{
"11111111": {
"id": "val_somevalue5555"
}
},
{
"2222222": {
"id": "val_somevalue25"
}
}
]';
$jsonarr = json_decode($json, true);
$newarr = [];
foreach($jsonarr as $key => $value) {
$newarr[] = ['key' => key($value), 'id' => current($value)['id']];
}
foreach($newarr as $key) {
echo 'key '.$key['key'] . PHP_EOL;
echo 'id '.$key['id'] . PHP_EOL;
}
If you remove what looks like embedded components in the $json string (otherwise it won't parse) then var_export the output of json_decode() you'll get this:
array (
0 => array (
11111111 => array (
'id' => 'val_somevalue5555',
),
),
1 => array (
2222222 => array (
'id' => 'val_somevalue25',
),
),
)
You have a double-nested array, hence...
foreach ($jsonarr as $obj) {
foreach ($obj as $name=>$value) {
print "$name = $value[id]\n";
break;
}
}
or you can reference the elements directly:
print $jsonarr[0]['11111111']['id'];
First, you are not accessing the deep enough before iterating.
If you call var_export($jsonarr); you will see:
array ( // an indexed array of subarrays
0 =>
array ( // an associative array of subarrays, access via [0] syntax (or a foreach loop that only iterates once)
11111111 => // this is the subarray's key that you want
array (
'id' => 'val_somevalue5555', // this is the value you seek from the id element of 1111111's subarray
'customer' =>
array (
32312 =>
array (
'name' => 'jane doe',
),
),
),
2222222 => // this is the subarray's key that you want
array (
'id' => 'val_somevalue25', // this is the value you seek from the id element of 2222222's subarray
'customer' =>
array (
32312234 =>
array (
'name' => 'jane doe',
),
),
),
),
)
Code: (Demo)
$jsonarr = json_decode($json, true);
$result=[];
// vvvv-avoid a function call (key()) on each iteration by declaring here
foreach($jsonarr[0] as $key=>$subarray){
// ^^^-drill down into the first level (use another foreach loop if there may be more than one)
$result[]=['key'=>$key,'id'=>$subarray['id']];
}
var_export($result);
Output:
array (
0 =>
array (
'key' => 11111111,
'id' => 'val_somevalue5555',
),
1 =>
array (
'key' => 2222222,
'id' => 'val_somevalue25',
),
)
p.s. If $jsonarr has more than one element in its first level, you should use a foreach() loop like this:
foreach($jsonarr as $array1){
foreach($array1 as $key=>$array2){
$result[]=['key'=>$key,'id'=>$array2['id']];
}
}

Merge multi-dimensional arrays and sum column values which share a common value in another column

I have 3 arrays for storing posts,comments, and likes.
These are the JSON strings:
//comments JSON (stores user and comment points)
$comments='[
{
"user": "5",
"points": "12"
},
{
"user": "2",
"points": "1"
},
{
"user": "3",
"points": "1"
}
]';
//likes(stores user and likes point)
$likes='[
{
"user": "1",
"points": 7
},
{
"user": "4",
"points": 4
},
{
"user": "3",
"points": 1
}
]';
//posts (stores user and post points)
$posts='[
{
"user": "1",
"points": "6"
},
{
"user": "3",
"points": "2"
},
{
"user": "2",
"points": "1"
}
]';
I convert these JSONs into arrays like this:
$comment_array = json_decode($comments,TRUE);
$like_array = json_decode($likes,TRUE);
$post_array = json_decode($posts,TRUE);
//echo '<pre>';
//print_r($comment_array);
//print_r($like_array);
//print_r($post_array);
//echo '</pre>';
Now, I'm trying to sum these points and save the result in a new array. It's not mandatory that a user should have entries in all the three arrays. It depends on whether a user has made a comment, post or like.
function mergeArrays($filenames, $titles, $descriptions) {
$result = array();
foreach ( $filenames as $key=>$name ) {
$result[] = array( 'filename' => $name, 'title' => $titles[$key], 'descriptions' => $descriptions[ $key ] );
}
return $result;
}
The above function can merge all the three arrays.
$merged= mergeArrays($comment_array, $like_array, $post_array);
echo '<pre>';
print_r($merged);
echo '</pre>';
However, each array after merging is stored as an index element.
How can I get a result something like this:
$result='[
{
"user": "1",
"points": "13"
},
{
"user": "2",
"points": "2"
},
{
"user": "3",
"points": "4"
},
{
"user": "4",
"points": "4"
},
{
"user": "5",
"points": "12"
}
]';
Considering your three arrays, this code will get you an array with: points, votes and diferent users.
Edit: Adding additional array and printing it to get the output desired by question.
$points = 0;
$uniqueUsers = array();
$votes = 0;
$users = 0;
$result = array();
//Comments
if (!empty($comment_array)) {
foreach ($comment_array as $item) {
if (!in_array($item['user'], $uniqueUsers)) {
array_push($uniqueUsers, $item['user']);
$result[$item['user']] = 0;
}
$votes ++;
$result[$item['user']] += $item['points'];
}
}
// Likes
if (!empty($like_array)) {
foreach ($like_array as $item) {
if (!in_array($item['user'], $uniqueUsers)) {
array_push($uniqueUsers, $item['user']);
$result[$item['user']] = 0;
}
$votes ++;
$result[$item['user']] += $item['points'];
}
}
// Posts
if (!empty($post_array)) {
foreach ($post_array as $item) {
if (!in_array($item['user'], $uniqueUsers)) {
array_push($uniqueUsers, $item['user']);
$result[$item['user']] = 0;
}
$votes ++;
$result[$item['user']] += $item['points'];
}
}
foreach ($result as $idUser=>$points) {
echo "\n";
echo "\n" . 'User: ' . $idUser;
echo "\n" . 'Points: ' . $points;
}
$results = array('users'=> count($uniqueUsers), 'votes'=>$votes, 'points'=> $points);
//print_r($results);
The solution using array_column, array_walk_recursive and array_values functions:
...
$comments = array_column($comment_array, 'points', 'user');
$likes = array_column($like_array, 'points', 'user');
$posts = array_column($post_array, 'points', 'user');
$list = [$comments, $likes, $posts];
$result = [];
array_walk_recursive($list, function($v, $k) use(&$result){
if (key_exists($k, $result)){
$result[$k]['points'] += $v;
} else {
$result[$k] = ['user' => $k, 'points' => $v];
}
});
$result = array_values($result);
print_r($result);
The output:
Array
(
[0] => Array
(
[user] => 5
[points] => 12
)
[1] => Array
(
[user] => 2
[points] => 2
)
[2] => Array
(
[user] => 3
[points] => 4
)
[3] => Array
(
[user] => 1
[points] => 13
)
[4] => Array
(
[user] => 4
[points] => 4
)
)
Do the following to get one array with summed points:
$collections = array(
'comments' => json_decode($comments,TRUE),
'likes' => json_decode($likes,TRUE);,
'posts' => json_decode($posts,TRUE),
);
$newArray = array();
foreach ($collections as $collection) {
foreach ($collection as $user) {
$newArray[$user->user] += $user->points;
}
}
There are two important points to make if you want to learn the "best" way to handle these types of operations.
Don't use iterated in_array() calls when isset() can be used instead. This is because isset() is much more efficient than in_array().
Use temporary keys to identify duplicate occurrences, then re-index your results when finished -- usually with array_values(), but this time I used array_multisort() to re-order the results AND re-index.
Code: (Demo)
$merged = array_merge(json_decode($comments, true), json_decode($likes, true), json_decode($posts, true));
$result = [];
foreach ($merged as $entry) {
if (!isset($result[$entry['user']])) {
$result[$entry['user']] = $entry;
} else {
$result[$entry['user']]['points'] += $entry['points'];
}
}
array_multisort(array_column($result, 'user'), $result);
// usort($result, function($a, $b) { return $a['user'] <=> $b['user']; });
// array_multisort() will outperform `usort()` in this case.
echo json_encode($result);
Output:
[{"user":"1","points":13},{"user":"2","points":2},{"user":"3","points":4},{"user":"4","points":4},{"user":"5","points":"12"}]
Decode each array and merge them together into a multi-dimensional array.
Iterate each subarray and determine if it is the first occurrence of the user. If so, retain the entire subarray. If not, only increase the points tally within that subarray.
When the loop is finished, sort by user ascending.
This is clean, direct, and readable.

How to get values from nested JSON data?

Here's my JSON code:
{
"query": {
"count": 2,
"created": "2013-04-03T09:47:03Z",
"lang": "en-US",
"results": {
"yctCategories": {
"yctCategory": {
"score": "0.504762",
"content": "Computing"
}
},
"entities": {
"entity": [
{
"score": "0.902",
"text": {
"end": "19",
"endchar": "19",
"start": "0",
"startchar": "0",
"content": "Computer programming"
},
"wiki_url": "http://en.wikipedia.com/wiki/Computer_programming"
},
{
"score": "0.575",
"text": {
"end": "51",
"endchar": "51",
"start": "41",
"startchar": "41",
"content": "programming"
}
}
]
}
}
}
}
and below is my PHP code
$json_o = json_decode($json,true);
echo "Json result:</br>";
echo $json; // json
echo "</br></br>";
echo "Value result:</br>";
$result = array();
//$entity = $json_o['query']['results']['entities']['entity'];
foreach ($json_o['query']['results']['entities']['entity'] as $theentity)
foreach ($theentity['text'] as $thetext){
$result[] = $thetext['content'];
}
print_r($result);
My expectation is to get the value of content in entity, which is "Computer programming" and "programming".
I already searching around, but still have found the solution yet.
The result of my PHP code is:
Array ( [0] => 1 [1] => 1 [2] => 0 [3] => 0 [4] => C [5] => 5 [6] => 5 [7] => 4 [8] => 4 [9] => p )
Use this loop
foreach ($json_o['query']['results']['entities']['entity'] as $theentity)
{
$result[] = $theentity['text']['content'];
}
http://codepad.viper-7.com/tFxh1w
Output Array ( [0] => Computer programming [1] => programming )
Change your foreach to:
$result = array();
foreach ($json_o['query']['results']['entities']['entity'] as $theentity) {
$result[] = $theentity['text']['content'];
}
print_r($result);
$theentity['text'] is an array of keys => value. You can just access key content instead of looping through all of the entries.
Another way you could do it (though it is a poor choice) is:
foreach($theentity['text'] as $key => $value) {
if( 'content' === $key ) {
$result[] = $value;
}
}
I provide this second example to demonstrate why the original code did not work.
Update
To access other properties like the yctCategories just do something similar
$categories = array();
foreach ($json_o['query']['results']['yctCategories'] as $yctCategory) {
$categories[] = $yctCategory['content'];
}
print_r($categories);
remove the second foreach and replace it with $result[] = $theentity['text']['content'];
using something like http://jsonlint.com/ might make it easier for you to see how the json is structured. alternatively just var_dump (or print_r) the output of your json_decode.
Use simple code:
$array = $json_o['query']['results']['entities']['entity'];
foreach($array as $v){
echo $v['text']['content'];
}

How to convert a PHP array to a specific Javascript format

I know this must be very basic but I really don't know how to solve this. I want to turn a php array to the following notation to be used inside a javascript script. These are countries which are passed to the js script in the initialization.
Source notation (PHP)
array(3) { [0]=> array(1) { ["code"]=> string(2) "AR" } [1]=> array(1) { ["code"]=> string(2) "CO" } [2]=> array(1) { ["code"]=> string(2) "BR" } }
Desired outcome (JS)
[ "AR", "FK","CO", "BO", "BR", "CL", "CR", "EC", "GT", "HN", "LT", "MX", "PA", "PY", "PE", "ZA", "UY", "VE"]
I can reformat the origin PHP array as desired, what I need to know is how to format it to get the desired outcome.
I am using the following code to pass the array to js:
echo "<script>var codes = " . json_encode($codes) . ";</script>";
Looks like the following would work for you:
<?php
$arr[0]['code'] = 'AR';
$arr[1]['code'] = 'CO';
$arr[2]['code'] = 'BR';
print_r($arr);
function extract_codes($var) { return $var['code']; }
print_r(array_map('extract_codes', $arr));
echo json_encode(array_map('extract_codes', $arr));
?>
Output:
Array
(
[0] => Array
(
[code] => AR
)
[1] => Array
(
[code] => CO
)
[2] => Array
(
[code] => BR
)
)
Array
(
[0] => AR
[1] => CO
[2] => BR
)
["AR","CO","BR"]
It works by mapping each of the two-letter codes down to a normal one-dimensional array, then passing it to json_encode.
Going with array_reduce:
$output = array_reduce($array, function($result, $item){
$result[] = $item['code'];
return $result;
}, array());
echo json_encode($output);
You need to loop through your PHP associative array and set the appropriate variables.
Like this:
$item = ''; // Prevent empty variable warning
foreach ($php_array as $key => $value){
if (isset($key) && isset($value)) { // Check to see if the values are set
if ($key == "code"){ $item .= "'".$value."',"; } // Set the correct variable & structure the items
}
}
$output = substr($item,'',-1); // Remove the last character (comma)
$js_array = "[".$output."]"; // Embed the output in the js array
$code = $js_array; //The final product

Categories