I have a fairly big array that I source from Facebook:
array(25) {
[0]=>
array(14) {
["id"]=>
string(31) "245226895508982_651884328176568"
["from"]=>
array(2) {
["name"]=>
string(16) "Madeleine Björs"
["id"]=>
string(15) "100002249777453"
}
["to"]=>
array(1) {
["data"]=>
array(1) {
[0]=>
array(2) {
["name"]=>
string(31) "Wohnung/WG in München gesucht!"
["id"]=>
string(15) "245226895508982"
}
}
}
Now what I want to do is go through the array and save ID, name and various other information from that array into a mysql database. However, to understand how to target specific information I tried to echo the data first.
$data = json_decode(file_get_contents('https://graph.facebook.com/'), true);
foreach($data as $item) {
echo $item['id'];
echo '<pre>'; var_dump($item);
}
This PHP code is based on various posts on Stackoverflow, however, the code returns nothing. May you please help me to target the arrays appropriately? You may check the enire array here: http://faculty-fight.de/milliondollaridea/facebook_session.php
Cheers!
foreach($array as $key=>$subArray)
{
foreach($subArray as $subKey=>subSubArray)
{
if(is_array($subSubArray))
{
foreach($subSubArray as $subSubKey=>$value)
{
if(is_array($value))
{
foreach($value as $valueKey=>$subValue)
{
/* your code /*
}
}
}
}
}
you can check for id values for example for 1st loop (if($subKey == "to")).
Related
I checked this question and answers:
How to group a multidimensional array by a particular subarray value?
He wanted to group results by 'level'. But how would you do it to group it by 'level' first and then by 'type'?
Its pretty straight forward. Loop through $items array. Get each item's level and type and if they are not set yet, initialize them with an empty array. Then just push the "cust" value into the array.
I have given the code below.
I am assuming "$items" is an array which contains the input.
$g = [];
foreach($items as $k => $v) {
$l = $v["level"];
$t = $v["type"];
$c = $v["cust"];
if(!isset($g[$l])) {
$g[$l] = [];
}
if(!isset($g[$l][$t])) {
$g[$l][$t] = [];
}
$g[$l][$t][] = [
"cust" => $c
];
}
var_dump($g);
The output of this code would be like below:
array(3) {
[1]=>
array(1) {
["standard"]=>
array(2) {
[0]=>
array(1) {
["cust"]=>
string(6) "XT8900"
}
[1]=>
array(1) {
["cust"]=>
string(6) "XT8944"
}
}
}
[3]=>
array(1) {
["premier"]=>
array(2) {
[0]=>
array(1) {
["cust"]=>
string(6) "XT8922"
}
[1]=>
array(1) {
["cust"]=>
string(6) "XT8816"
}
}
}
[7]=>
array(1) {
["standard"]=>
array(1) {
[0]=>
array(1) {
["cust"]=>
string(6) "XT7434"
}
}
}
}
[P.S.]: You can also use sort to solve this problem easily. That's another way of solving this problem.
Using PHP and MySQL, I have generated an array called $response.
A var_dump of $response can be seen here.
array(2) {
["OperationRequest"]=>
array(4) {
["HTTPHeaders"]=>
array(1) {
[0]=>
array(1) {
["#attributes"]=>
array(2) {
["Name"]=>
string(9) "UserAgent"
["Value"]=>
string(14) "ApaiIO [2.1.0]"
}
}
}
["RequestId"]=>
string(36) "f53f381e-efb3-4fef-8e39-4f732b4b463e"
["Arguments"]=>
array(1) {
["Argument"]=>
array(11) {
[0]=>
array(1) {
["#attributes"]=>
array(2) {
["Name"]=>
string(14) "AWSAccessKeyId"
["Value"]=>
string(20) "KEY"
}
}
[1]=>
array(1) {
["#attributes"]=>
array(2) {
["Name"]=>
string(12) "AssociateTag"
["Value"]=>
string(11) "TAG"
}
}
[2]=>
array(1) {
["#attributes"]=>
array(2) {
["Name"]=>
string(6) "IdType"
["Value"]=>
string(4) "ISBN"
}
}
[3]=>
array(1) {
["#attributes"]=>
array(2) {
["Name"]=>
string(6) "ItemId"
["Value"]=>
string(38) "0751538310,9780141382067,9781305341141"
}
}
[4]=>
array(1) {
["#attributes"]=>
array(2) {
["Name"]=>
string(9) "Operation"
["Value"]=>
string(10) "ItemLookup"
}
}.......so on
A json_encode of the array can be seen here (as requested in a comment).
I'd like to select the Title from these two items. From what I can see this is located at;
Items > Item > ItemAttributes > Author
So, using a foreach loop I have tried the following;
foreach ($response as $item) {
echo $item['Items']['Item']['ItemAttributes']['Title']; // line 2
}
However this returns the following error;
Message: Undefined index: Items. Line Number: 2
Where am I going wrong and what must I change in my code in order to achieve the desired result?
Also, any advice on how to 'read' multidimensional arrays would be greatly appreciated.
Thanks
Try this one, it will help you out. You were are iterating on the wrong key that's why you were not getting desired output.
Try this code snippet herefrom json provide by OP in question
foreach($array["Items"]["Item"] as $key => $value)
{
print_r($value["ItemAttributes"]["Title"]);
echo PHP_EOL;
}
Output:
Panic
Panic
Captain Flinn and the Pirate Dinosaurs: Missing Treasure! (Captain Flinn)
For getting unique titles:
foreach(json_decode($json,true)["Items"]["Item"] as $key => $value)
{
$result[]=$value["ItemAttributes"]["Title"];
echo PHP_EOL;
}
print_r(array_unique($result));
#Also, any advice on how to 'read' multidimensional arrays would be greatly appreciated.
Post your encoded json string to
http://json.parser.online.fr
"+" and "-" button at the right panel should help you read it easily.
//Check Items is not empty
if( !isset($response["Items"]["Item"]) || empty($response["Items"]["Item"]) )
throw New Exception("Empty Item");
foreach($response["Items"]["Item"] as $item){
$title = $item['ItemAttributes']['Title']
}
You should debug as:
foreach ($response as $key => $item) {
if(isset($item['Items'])){ //Check Items index defined
echo $item['Items']['Item']['ItemAttributes']['Title'];
}else{
var_dump($key);
}
}
I have a PHP Loop that loops through data, i want to be able to echo all of this data alphabetically.
i currently use an array:
$output[]=array();
foreach($WHM_Servers as $whm) {
foreach($server->list_accounts() as $ret) {
$output[]= '<tr class="notfirst">
<td>'.$ret["domain"].'</td>
<td>'.$ret["user"].'</td>
<td>'.CompanyInfoLookup($result["customer"], 'company').'</td>
<td>'.$ret["startdate"].'</td>
<td>'.$ret["starttime"].'</td>
<td>'.$ret["disklimit"].'</td>
<td>'.$ret["diskused"].'</td>
</tr>';
}
}
would i be able to add a key in the array and then echo the array alphabetically from the keys
A few different ways to do this, I chose to write a user defined sort function.
$accounts = array();
$accounts[]['name'] = "Tom";
$accounts[]['name'] = "Frank";
$accounts[]['name'] = "Zed";
$accounts[]['name'] = "Aaron";
function compareNames($a, $b){
return strcmp($a['name'], $b['name']);
}
usort($accounts, "compareNames");
var_dump($accounts);
Output:
array(4) {
[0]=>
array(1) {
["name"]=>
string(5) "Aaron"
}
[1]=>
array(1) {
["name"]=>
string(5) "Frank"
}
[2]=>
array(1) {
["name"]=>
string(3) "Tom"
}
[3]=>
array(1) {
["name"]=>
string(3) "Zed"
}
}
This is a standalone example. To apply it to your example, you need to store the data: $accounts = $server->list_accounts(), sort it: usort($accounts, "compareNames");, and then pass it in to your loop: foreach($accounts as $ret) {
I have the following php code that displays csv data on the browser:
$file1 = file('SpreadsheetA.csv',FILE_IGNORE_NEW_LINES);
foreach($file1 as $val)
{
echo $val;
}
the above outputs all the data in the csv file as a string:
Matter Number,Amount,Currency,Company Code100,2000,USD,310101,23000,EUR,110102,120,GBP,120103,10000,USD,310
if i want to capture the above as an array, this is what i do:
foreach($file1 as $val)
{
var_dump(array($val));
}
and this is the output:
array(1) {
[0]=>
string(42) "Matter Number,Amount,Currency,Company Code"
}
array(1) {
[0]=>
string(16) "100,2000,USD,310"
}
array(1) {
[0]=>
string(17) "101,23000,EUR,110"
}
array(1) {
[0]=>
string(15) "102,120,GBP,120"
}
array(1) {
[0]=>
string(17) "103,10000,USD,310"
}
as shown, each string is captured as an array..my wish is to capture all strings under a single array as follows:
array(5) {
[0]=>
string(42) "Matter Number,Amount,Currency,Company Code"
[1]=>
string(16) "100,2000,USD,310"
[2]=>
string(17) "101,23000,EUR,110"
[3]=>
string(15) "102,120,GBP,120"
[4]=>
string(17) "103,10000,USD,310"
}
how would i accomplish the above(inside the foreach loop)??
you should loop your arrays into $file and fill another array with the string values
try this code:
$output = array();
foreach ($file1 as $val) {
$output[] = $val[0];
}
var_dump(array($output));
hi i need to get the values of a var that after making it a var_dump($var) i get:
array(1) {
["docs"]=> array(3) {
[0]=> array(1) {
["imgurl"]=> string(68) "http://xxxxx.com/demos/grider/wp-content/uploads/2013/02/22.jpg" }
[1]=> array(1) {
["imgurl"]=> string(68) "http://xxxxx.com/demos/grider/wp-content/uploads/2013/02/33.jpg" }
[2]=> array(1) {
["imgurl"]=> string(68) "http://xxxxx.com/demos/grider/wp-content/uploads/2013/02/22.jpg" } } }
I need to foreach the 2 img url strings starting with http://
Any help would be appreciated.
Thank you
This should work:
foreach($var['docs'] as $sub) {
echo $sub['imgurl'];
}
foreach ($arr['docs'] AS $key => $link) {
echo $link['imgurl'].'<br />';
}