Decode the json array and break with foreach loop - php

I am trying to decode json and then break the value with foreach loop.
Here is my script.
<?php $new_data=json_decode($pay_data->l1_level,true);
print_r($new_data);
?>
this print return array.
Array ( [name] => Array ( [0] => sanjay [1] => susanchen [2] => mabelzhen) [product-price] => Array ( [0] => 250 [1] => 250 [2] => 250) )
I am trying to break this foreach loop and get name and price value . I am trying to this away
foreach($new_data as $value){
echo $value->name;
echo $value->product_price ;
}
But This is not working.Any Help will be Appreciate.

When you use foreach($new_data as $value), the $value doesn't have the key.
Please try the following code.
foreach($new_data as $value){
print_r($value);
}
the result is
Array ( [0] => sanjay [1] => susanchen [2] => mabelzhen ) Array ( [0] => 250 [1] => 250 [2] => 250 )

Related

PHP Loop through array with no index names

In PHP I have this structure of Array (some are empty, some not, some are multiple items):
Array
(
[0] => Array
(
)
[1] => Array
(
[0] => 16534
)
[2] => Array
(
)
[3] => Array
(
[0] => 16532
[1] => 16533
)
[4] => Array
(
)
[5] => Array
(
[0] => 14869
)
}
I want to loop through this array, so as the result I get only the numbers (all of them).
I tried it this way:
foreach ($myarray as $item) {
echo '<pre>' . print_r($item) . '</pre>';
// $result[] = $this->myMethod($item);
}
So in foreach I want to use all the items from array in my method.
However when I echo the $item in the loop, I have something like this:
Array ( )
Array ( [0] => 16534 )
Array ( )
Array ( [0] => 16532 [1] => 16533 )
Array ( )
Array ( [0] => 14869 )
So still arrays (also the empty ones), and not numbers.
Can you please help with this?
UPDATE: I just noticed, some of the arrays looks like this:
[6] => Array
(
[0] => Array
(
[id] => 269
[hours] => 21.0
)
)
[7] => Array
(
[0] => Array
(
[0] => Array
(
[id] => 2
[hours] => 12.0
)
[1] => Array
(
[id] => 7
[hours] => 24.0
)
)
[1] => Array
(
[0] => Array
(
[id] => 2
[hours] => 5.0
)
[1] => Array
(
[id] => 7
[hours] => 0.583
)
)
)
but here the solution works not.
This one seems working but it is now brutal foreach in foreach solution:
foreach ($myarray as $item2) {
foreach ($item2 as $key2 => $val2) {
if (isset($val2)) {
foreach ($val2 as $key4 => $val4) {
echo $val4['id'].',';
}
}
}
}
Just merge the array which will also remove the empties:
foreach(array_merge(...$myarray) as $item) {
// echo '<pre>' . print_r($item) . '</pre>';
$result[] = $this->myMethod($item);
}
This will also work and may be faster:
$result = array_map([$this, 'myMethod'], array_merge(...$myarray));
If you have an old PHP version you'll have to use array() instead of [] and:
call_user_func_array('array_merge', $myarray)
You are only looping through the main array.That's the reason why you are getting an array when you are printing the result set.(because those values are stored in sub arrays and you are not looping them.)
And to remove the sub arrays with empty values I'll use isset() so that you will get only the values.
Change your code into this.
foreach ($myarray as $item) {
foreach($item as $key=>$val){
if(isset($val){
$values[] = $val;
// $result[] = $this->myMethod($values);
}
}
}

php array data sorting

Have troubles with sorting data in array, after I loop through a table in my database these are result:
Array
(
[0] => Array
(
[model] => Cars
[number] => 101
)
[1] => Array
(
[model] => Cars
[number] => 113
)
[2] => Array
(
[model] => Train
[number] => 220
)
)
From data above, how do I get them to be like below?
Array
(
[0] => Cars
(
[0] => 101
[1] => 113
)
[2] => Train
(
[0] => 220
)
)
Thank you.
if cars,trains ... are in a column in your table you can use GROUP BY to get data from database and then use it like that array you want.
if not use this loop :
$array; //your array
$new_array=[];
foreach($array as $i => $item){
if(!array_key_exists($item[model],$new_array))
$new_array[$item[model]]=[];
$new_array[$item[model]][]=$item[number];
}
You can use array_reduce to group your array.
Note: I used model as the key of the $result
$arr = //your array
$result = array_reduce($arr, function($c, $o){
$c[$o['model']][] = $o['number'];
return $c;
},array());
echo "<pre>";
print_r( $result );
echo "</pre>";
This will result to:
Array
(
[Cars] => Array
(
[0] => 101
[1] => 113
)
[Train] => Array
(
[0] => 220
)
)
Doc: array_reduce()

Inserting calculated values during a for each loop

I have an array that looks like this.
Array
(
[0] => Array
(
[0] => 1
[1] => 500
[2] => 800
)
[1] => Array
(
[0] => 1
[1] => 100
[2] => 200
)
[2] => Array
(
[0] => 1
[1] => 300
[2] => 400
)
)
I want to use a foreach loop (not a function) to do several things. First I just want to subtract subelement [1] from subelement [2] to generate sub element [3] and then insert subelement3 back into the output array. See below:
$output=
Array
(
[0] => Array
(
[0] => 1
[1] => 500
[2] => 800
[3] => 300
)
[1] => Array
(
[0] => 1
[1] => 100
[2] => 200
[3] => 100
)
[2] => Array
(
[0] => 1
[1] => 400
[2] => 250
[3] => 150
)
)
To do the above I am using the for each loop below to generate the values of subelement 3 and it works ok. My problem is inserting the values back into the output array in the right place. I have commented out my last attempt which failed. Sorry if I am missing the obvious here.
foreach($result as $sub)
{
//get values
$sub[3]=$sub[1]-$sub[2];
echo "<difference>".$sub[3]."<br>";//works ok
//insert values back into array
//$result[$sub[0]][3] = $sub[3];
}
print "<pre>";
print_r($result);
print "</pre>";
die();
The problem:
$result[$sub[0]][3] = $sub[3]; //= $result[1][3] = $sub[3]
$sub[0] always return 1 (according to your input array) so it's not what you're looking for.
The solution:
Use the $array as $key => $val format in your foreach loop
and then access the sub array according to the relevant key.
foreach($result as $key => $sub)
{
//get values
$result[$key][3]=$sub[1]-$sub[2];
You can use array_map too, it's more elegant:
$output = aray_map( function ($item) {
$item[] = $sub[2] - $sub[1];
return $item;
}, $arr);
Demo

echo array value return form facebook api php

i am trying to display array values return from facebook api. array is stored in $result below is my array
Array
(
[data] => Array
(
[0] => Array
(
[name] => Ravi Shankar
[id] => 10206576743272319
)
)
[paging] => Array
(
[next] => https://graph.facebook.com/v2.5/1705944052976370/friends?limit=25&offset=25&__after_id=enc_AdBV9ZBlJwwdjBL8iWeIAZBSxDqJO0gvQWS45qwBBg1X8tCbZAoj9Cz506ZCGuDnddOL07MZD
)
[summary] => Array
(
[total_count] => 4628
)
)
i tried using foreach but confused
foreach($result as $key=>$value){
echo $key."-".$value."<br />";
}
i want to display the result like below:
name id
xxxx 123456
yyyy 173453
Write your foreach loop as below:-
foreach($result['data'] as $key=>$value){
echo $value['name'].'-'.$value['id']."<br>";
}
Hope it will help you :)
You need to use foreach in the $result["data"]:
foreach ($result["data"] as $friend) {
echo "{$friend["id"]} => {$friend["name"]}";
}
The each $friend variable is an array, of this format:
Array
(
[name] => Ravi Shankar
[id] => 10206576743272319
)

How to loop through json array within an array php

I need help traversing an array within array, I only need to loop through certain arrays, for example, how would I just lop through the names array?
Array
(
[#total_records] => 10
[#total_matching_records] => 10
[#available_records] => 200
[#available_matching_records] => 12
[query] => Array
(
[summary] => Array
(
[emails] => Array
(
[0] => Array
(
[content] => jonathan.lyon#gmail.com
)
)
)
)
[results] => Array
(
[person] => Array
(
[#match_score] => 1
[summary] => Array
(
[names] => Array
(
[0] => Array
(
[first] => Jonathan
[last] => Lyon
[display] => Jonathan Lyon
)
[1] => Array
(
[first] => Jonathan
[last] => Jordan
[display] => Jonathan Jordan
)
)
I have tried this but can't get it to work:-
foreach($json_output['results']['person']['summary']['names'] as $key => $val) {
echo $key.": ".$val."</br>";
}
Any help would be greatly appreciated.
Thanks
Jonathan
In your example you trying to echo $key. Key in your case $key is array index (integer). Are you sure you realy need that?
You nedd to change your code to:
foreach($json_output['results']['person']['summary']['names'] as $val) {
echo $val['display']."</br>";
}
Have you tried this
foreach($json_output['results']['person']['summary']['names'] as $key => $val) {
echo $key.": ".$val['display']."</br>";
}
?
Are you getting any error output? That would help a lot. I can see also that $val in this case is an array so you don't want to echo that.

Categories