Iterate through Array and extract data [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am trying to iterate through this array http://2of1.com/zee/ZEES%20SMS%20SERVICE.html and extract the data.
I was using:
foreach ($graphObject['data'] as $key => $value){
$string = $value->message;
$link = $value->actions[0]->link;
$pic = $value->picture;
$post_id = $value->id;
}
But it is no longer working after i added a second source to the array.
When i try:
foreach ($graphObject as $key => $value){
$string = $value->data[0]->message;
$link = $value->data[0]->actions[0]->link;
$pic = $value->data[0]->picture;
$post_id = $value->data[0]->id;
I get only the first entry values from data[0] and it does not iterate through all the data. What i need is the data from data[0] data[1] data[2] data[3]... etc etc... Please help. Thank you!

Probably something like
foreach ($graphObject as $keyEntry => $entry){
foreach( $entry->data as $data ) {
echo $data->picture, "<br />\r\n";
foreach( $data->actions as $action ) {
echo $action->link, "<br />\r\n";

Related

Decode JSON each array to PHP [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have some encoded json like this:
[
{"title":"root", "link":"one"},
{"title":"branch", "link":"two"},
{"title":"leaf", "link":"three"}
]
I want to decode that JSON into PHP output like:
title || link
root || one
branch || two
leaf || three
I tried this but doesn't work:
$list = json_decode($json);
foreach ($list as $list => $value) {
echo $list->title;
echo $list->link;
}
try changing your foreach loop to this.
foreach ($list as $key => $value) {
echo $value->title." || ";
echo $value->link." ";
echo nl2br("\n");
}
Hope this Works for you.
What you did is looping the keys and values seperated and than you tried to get the values from the keys of the stdClass, what you need to do is looping it as an object. I also used json_decode($json_str, true) to get the result as an array instead of an stdClass.
$json_str = '[{"title":"root","link":"one"},{"title":"branch","link":"two"},{"title":"leaf","link":"three"}]';
$json_decoded = json_decode($json_str, true);
foreach($json_decoded as $object)
{
echo $object['title'];
echo $object['link'];
}
Code :
$list = json_decode($json);
foreach ($list as $item) {
echo $item->title . ' || ' . $item->link . '<br>';
}

How to use foreach array value out side of loop [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
foreach($reciveValue as $value){
echo $value.",";// Result: based on user input like:10,11,12,13,14
}
echo $value; // Result: 14
the result inside loop is: 10,11,12,13,14
and outside loop is : 14
I want to use all value outside of loop
Use below code to get your array values comma seperated
implode(",", $reciveValue)
Try this :
$value = '';
foreach($reciveValue as $val){
$value .= $val.",";// Result: based on user input like:10,11,12,13,14
}
echo rtrim($value, ',');
#Support Techcherry if you want to use all values of an array outside
the loop then you can use implode() function, below is an example
understand
<?php
$reciveValue = array(10,11,12,13,14); // suppose this is your array
foreach($reciveValue as $value){
echo $value.",";// Result: based on user input like:10,11,12,13,14
}
echo "<br>";
echo implode(',', $reciveValue); // here implode() function convert the array value in the string form
?>
try it, it will help you
thanks to everyone for posting answer
every answer is very helpful for me
but finally i got my solution
$result="";
foreach($reciveValue as $value)
{
$result=$value.",".$result;
}
echo $result;

How to delete an array of array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an array like this :
[[a,b],[c,d]]
How can I delete the outer array so it becomes:
[a,b],[c,d]
I tried using unset but cannot find a solution.
i got this answer by asign arrays in loops to another variable.. so how can i access the loops outside the loop it self?
foreach($arr3 as $key => $value)
{
$newArr[$key] = $value;
echo json_encode($value); // this will answer my question
}
echo json_encode($value); // when i echo outside loop it will not display as inside loop
Simple way to do this is as below.
foreach($array as $key => $value)
{
$newArr[$key] = $value;
}
$newArr contains new array which you are asking.
Comment Response
You can also con cat it.
$concat = "";
foreach($arr3 as $key => $value)
{
$newArr[$key] = $value;
$concat .= json_encode($value).',';
}
echo rtrim($concat,',');
You should not delete the outer array. You can simply access the index of the array and cast it to another array.
As an example;
$arr = array(array('x','y'),array('z'));
You can access this with;
$arr[0];
$array2=$array[0];
$array3=$array[1];
unset($array);

Get data from mysql as array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Sorry for my english..
in mysql has row name iurl.. There is data: 1365269423.jpg,1365270586.jpg,1365270666.jpg,1365270683.jpg
i get its as:
<?php $s=mysql_query("select iurl from points where id='".$_GET['id']."' ");
if($s){
$array = array();
while($t=mysql_fetch_array($s)) {
$array[] = $t['iurl'];
}
print_r($array);
?>
it gives me result: Array ( [0] => 1365269423.jpg,1365270586.jpg,1365270666.jpg,1365270683.jpg )
And i`m need get it and print like a link
how can i do it?
Thanks..
You can use explode(); to split the string into an array and then loop over to print each item:
$images = explode(",", $t["iurl"]);
foreach ($images as $image) {
echo "{$image}";
}
I think you are asking to have result or in your case a .jpg file be the href of your link? If so do this:
......
The result you get from your database as you already figured out is an Array
The only thing you need to do is loop trough the array.
ps: consider using mysqli more info at php.net http://www.php.net/manual/en/book.mysqli.php
<?php
$s=mysql_query("select iurl from points where id='".$_GET['id']."' ");
while ($row = mysql_fetch_object($s)) {
echo '<img="http://yourhost.com/images/'.$row->image.' alt=""/>';
}
?>

how to get values from the content? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
{"prereqs":{"prereq":{"type":"prereq_check","value":"submerging_island_feature_enabled"}},"divisions":{"division":[{"items":{"item":[{"name":"rhino_shell","rarity":"common"},{"name":"walrus_wavy","rarity":"special"},{"name":"hippo_fancyshell","rarity":"rare"},{"name":"rhino_jellyfish","rarity":"superRare"}]},"name":"rubyCount_30"},{"items":{"item":[{"name":"walrus_clam","rarity":"common"},{"name":"hippo_nautical","rarity":"special"},{"name":"giraffe_coral","rarity":"rare"},{"name":"elephant_starburst","rarity":"superRare"}]},"name":"rubyCount_40"},{"items":{"item":[{"name":"giraffe_waverider","rarity":"common"},{"name":"pony_sea","rarity":"special"},{"name":"magicdeer_seadeer","rarity":"rare"},{"name":"pony_seaprincesscorn","rarity":"superRare"}]},"name":"rubyCount_50"},{"items":{"item":[{"name":"bigcat_crystallion","rarity":"common"},{"name":"magicdeer_midnightdeer","rarity":"special"},{"name":"horse_ofthesea","rarity":"rare"},{"name":"horse_wingedsea","rarity":"superRare"}]},"name":"rubyCount_60"}]},"crafting":{"recipes":{"recipe":[{"name":"qdke"},{"name":"sb1p"},{"name":"cb8v"}]}},"listEndDate":"07/13/2015","currencyItem":{"name":"healingpotionbottle"},"feed":{"throttleTime":"21600"},"name":"submerging_island"}
To get you going my 2 cents. First off, Welcome, please read How to ask a good question
First you need to decode the json string in to an array. with that array you can get the values.
<?php
$json = '{"prereqs":{"prereq":{"type":"prereq_check","value":"submerging_island_feature_enabled"}},
"divisions":{"division":[{"items":{"item":[{"name":"rhino_shell","rarity":"common"},
{"name":"walrus_wavy","rarity":"special"},{"name":"hippo_fancyshell","rarity":"rare"},
{"name":"rhino_jellyfish","rarity":"superRare"}]},"name":"rubyCount_30"},
{"items":{"item":[{"name":"walrus_clam","rarity":"common"},{"name":"hippo_nautical","rarity":"special"},
{"name":"giraffe_coral","rarity":"rare"},{"name":"elephant_starburst","rarity":"superRare"}]},"name":"rubyCount_40"},
{"items":{"item":[{"name":"giraffe_waverider","rarity":"common"},{"name":"pony_sea","rarity":"special"},
{"name":"magicdeer_seadeer","rarity":"rare"},
{"name":"pony_seaprincesscorn","rarity":"superRare"}]},"name":"rubyCount_50"},
{"items":{"item":[{"name":"bigcat_crystallion","rarity":"common"},{"name":"magicdeer_midnightdeer","rarity":"special"},
{"name":"horse_ofthesea","rarity":"rare"},
{"name":"horse_wingedsea","rarity":"superRare"}]},"name":"rubyCount_60"}]},"crafting":{"recipes":{"recipe":[{"name":"qdke"},
{"name":"sb1p"},{"name":"cb8v"}]}},"listEndDate":"07/13/2015","currencyItem":{"name":"healingpotionbottle"},"feed":{"throttleTime":"21600"},"name":"submerging_island"}';
//decode the json
$decoded = json_decode($json, true);
// uncomment if you want it to be easier to read
// echo "<pre>";
// print_r($decoded);
// echo "</pre>";
//if you want the singe value, i.e. name of the first item.
echo "If you want a sinle value:<br>";
echo $decoded['divisions']['division'][0]['items']['item'][0]['name'] . "<br>";
//to get all names from one item you need to use a foreach() loop.
echo "<br>if you want all names from one item:<br>";
foreach($decoded['divisions']['division'][0]['items']['item'] AS $value){
echo $value['name'] . "<br>";
}
//to get all names we need to use 2 foreach loops because this is nested in multiple arrays
echo "<br>if you want all names from all items:<br>";
foreach($decoded['divisions']['division'] AS $value){
foreach($value['items']['item'] AS $value){
echo $value['name'] . "<br>";
}
}
?>

Categories