Multi Level Array - seems like I am going around in circles? - php

This is an example of the array I am working with
array(7)
{
["ClassId"]=> int(26)
["ClassName"]=> string(9) "Candidate"
["Data"]=> array(1)
{
[0]=> array(8)
{
["AppDataId"]=> int(17736)
["FirstName"]=> string(4) "hano"
["LastName"]=> string(11) "steenhuizen"
["CvTxtField"]=> string(4) "coal"
["Telephone"]=> string(6) "2345§"
["Email"]=> string(27) "hano11aaaaa#steenhuizen.com"
["Abstract"]=> string(16) "hano steenhuizen"
["TimeStamp"]=> string(22) "2017-09-05 06:08:41+02"
}
}
["RowCount"]=> int(1)
["PageNumber"]=> int(1)
["PageSize"]=> int(100)
["QueryTime"]=> string(6) "0.009s"
}
For the life of me, I just cannot loop this with a basic PHP foreach loop? $objApi contains the array above
echo '<table>';
foreach($objApi as $value)
{
echo '<tr><td>' . $value['FirstName'] . '</td></tr>';
}
echo '</table>
I would love to understand the workings of the array better as for some reason I just cannot get it right.

The arrays is a tree of values associated to a key, you can define the key and the values as you with, even, you can create a value of a array as another array. The only thing that you have to know is the structure that you array has, at the moment to iterate.
For you example code, if you want to iterate the data result of your query, this is the way:
foreach($row['Data'] as $row){
foreach($row as $user){
echo '<tr><td>'.$user['FirstName'].'</td></tr>';
}
}
I access directly in the array key that has the values of your query

Do:
foreach($objApi as $value)
{
print_r($value);
}
Then check if there is a need for another inside loop.
It seems that you might need:
foreach($value["Data"] as $data)
{
print_r($data);
}
Then you can use $data['FirstName']

echo '<table>';
foreach($objApi['Data'] as $value)
{
echo '<tr><td>' . $value['FirstName'] . '</td></tr>';
}
echo '</table>';

Related

Loop through an array returned by vafpress

I know how to perform a normal foreach loop but I can't seem to work it for an array returned by the vafpress framework. I am using var_dump(vp_metabox('vp_meta_sample_2.binding_group')); which is generating the below mentioned array. How can I loop through all the images!
(1) { [0]=> array(4) { ["name"]=> string(1) "1" ["url"]=> string(10) "234234.com" ["image"]=> string(62) "http://localhost/wp/wp-content/uploads/2014/02/bottomright.jpg" ["shortcode"]=> string(108) "[shortcode name="1" url="234234.com" image="http://localhost/wp/wp-content/uploads/2014/02/bottomright.jpg"]" } }
I am using the following loop to get the values
$age=print_r (vp_metabox('vp_meta_sample_2.binding_group'));
print_r ($age);
foreach($age as $x=>$x_value)
{
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
In this statement:
$age=print_r (vp_metabox('vp_meta_sample_2.binding_group'));
from the code above, $age will be empty since print_rdoes not return anything.
Proper:
$age= vp_metabox('vp_meta_sample_2.binding_group');
Try this, without print_r()
$age=vp_metabox('vp_meta_sample_2.binding_group');
instead of
$age=print_r (vp_metabox('vp_meta_sample_2.binding_group'));

PHP foreach loop through array

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")).

Selecting data from var_dump?

I ran a var_dump on my variable that is a list of information, here is the result of the var_dump
array(2) { ["name"]=> string(3) "top" ["value"]=> string(4) "100%" }
array(2) { ["name"]=> string(4) "left" ["value"]=> string(3) "Gus" }
array(2) { ["name"]=> string(4) "text" ["value"]=> string(4) "Hank" }
How do I get the value of the [name] => "top" ([value] here is 100%)so on and so forth?
Here is the PHP
foreach ($field['options'] as $key => $value) {
echo '<div style="color: #fff;">';
echo '<li style="color: #fff;">'.var_dump ($value).'</li>';
echo '</div>';
}
To get "top" I tried $value['top'] just like $field['options'] gets the options array, how do I break it down to get each speific option?
You mis-use var_dump($value) .
Assume $field['options'] is the array you var_dump at the beginning, you can just use $value['name'] instead of var_dump($value).
To find specific value, use something like if($value['name'] === 'top') in the foreach loop
Sidenote: The function var_dump() is to print the variable content. To get it inline / into a variable, use var_export($variable, true).
Use $value['name'] and $value['value'] to get the respective values.

Find matching items in array

Absolutely doing my head in here over something that I'm sure is very simple...
I have 2 arrays.
$post_cats which are categories that any given post is in.
$ad_cats which is an array of categories in which ads are placed.
Basically, if a post has in its array of selected categories, a category that matches an item in the array of ad categories, then it must return the matching value/item.
$post_cats returns this
array(4) {
[0]=> array(1) { ["slug"]=> string(6) "energy" }
[1]=> array(1) { ["slug"]=> string(6) "global" }
[2]=> array(1) { ["slug"]=> string(8) "identify" }
[3]=> array(1) { ["slug"]=> string(5) "south" }
}
and $ad_cats returns this
array(6) {
[0]=> array(1) { ["slug"]=> string(5) "north" }
[1]=> array(1) { ["slug"]=> string(5) "south" }
[2]=> array(1) { ["slug"]=> string(4) "east" }
[3]=> array(1) { ["slug"]=> string(4) "west" }
[4]=> array(1) { ["slug"]=> string(6) "global" }
[5]=> array(1) { ["slug"]=> string(8) "fallback" }
}
The duplicated item there is "south", so in my mind the value of array_intersect($post_cats, $ad_cats); should be an array with a single item - "south", correct?
But its returning, what seems like, everything in either of the arrays... I can't for the life of me get it to work..
Using the above example, I need to return "south" to a variable.
So you are looking for items that are in both arrays? ...
What about something like this:
function find_duplicate($array1, $array2)
{
$list = array();
foreach($array1 as $value1)
{
foreach($array2 as $value2)
{
if($value1 == $value2) $list[] = $value1;
}
}
return $list;
}
The best way is to convert those arrays in arrays array_intersect can work with.
Considering:
$a; // first array
$b; // second array
then you would go with:
$a1 = array();
foreach ($a as $v) $a1[] = $v['slug'];
$b1 = array();
foreach ($b as $v) $b1[] = $v['slug'];
$c = array_intersect($a1, $b1);
PHP functions usually work with more powerful algorithms than what you may think; therefore it's a good choice to let PHP functions handle this kind of things.
This solution uses array_map to get at the values and takes the intersection of that
function mapper($a)
{
return $a['slug'];
}
$set1 = array_map('mapper', $post_cats);
$set2 = array_map('mapper', $ad_cats);
$result = array_intersect($set1, $set2);
PhpFiddle for testing.

How to loop through an array of objects that have been decoded from JSON in PHP, and echo the values

I'm new to PHP and am not sure how to proceed. The array that I get back from decoding the JSOn is: (sorry if its formatted weird)
array(3) {
[0]=> array(4) {
["Name"]=> string(22) "Brent's Medical Center"
["date"]=> string(26) "/Date(1330449077600-0700)/"
["dealType"]=> string(13) "Capital Lease"
["id"]=> string(11) "MO-N007175A"
}
[1]=> array(4) {
["Name"]=> string(22) "Brent's Medical Center"
["date"]=> string(26) "/Date(1330448929213-0700)/"
["dealType"]=> string(2) "NA" ..... ["id"]=> string(11) "MO-N007172Q" } [2]=> array(4) { ["Name"]=> string(15) "MOC" ["date"]=> string(28) "/Date(-62135571600000-0700)/" ["dealType"]=> string(2) "NA" ["id"]=> string(9) "MC" } }
I have used this foreach loop, but am not sure how to get each individual item out of an associative array.
foreach ($obj as $key => $value) {
print_r($key);
}
This returns:
012
I have tried other solutions, but to no avail. Maybe I'm not understanding completely what's happening, but I can't get anything to do what I need/want.
Thanks!
Well, it depends exactly how you want it returned.
foreach ($obj as $key => $value) {
print_r($value);
}
Would return your data like this:
array(4) {
["Name"]=> string(22) "Brent's Medical Center"
["date"]=> string(26) "/Date(1330449077600-0700)/"
["dealType"]=> string(13) "Capital Lease"
["id"]=> string(11) "MO-N007175A"
}
array(4) {
["Name"]=> string(22) "Brent's Medical Center"
["date"]=> string(26) "/Date(1330448929213-0700)/"
["dealType"]=> string(2) "NA"
["id"]=> string(11) "MO-N007172Q"
}
... etc
If you wanted individual data pieces via your example, it would be like this:
foreach ($obj as $each_array) {
foreach ($each_array as $val){
echo $val . "<br>";
}
}
Which would return:
Brent's MedicalCenter
/Date(1330449077600-0700)/
Capital Lease
... etc
You have nested objects, try the following:
echo '<table>';
foreach ($obj as $key => $value) {
echo '<tr>';
echo '<td>' . $value->Name . '</td>';
echo '<td>' . $value->date . '</td>';
echo '<td>' . $value->dealType . '</td>';
echo '<td>' . $value->id . '</td>';
echo '</tr>';
}
echo '</table>';
Assuming that $data is what was var_dump'd in your pasted content:
foreach($data as $record) {
//$record['name'] is now something like "Brent's medical center"
}
Note though that you'll have to process the date field into something more usable than a string.
Script echo's exactly what you ask it to echo - array keys(indexes). Array has 3 values, so its keys are 0, 1, 2.
Looks like you need $value variable insode foreach-loop.
I hope it helps.
Here's a simplified version of your problem. Substitute your array for the one here.
<?php
$arr[0] = array('uno'=>'one', 'dos'=>'two');
$arr[1] = array('AAAA'=>'aaaa', 'BBBB'=>'bbbb');
foreach ($arr as $obj) {
foreach ($obj as $k=>$v) {
echo "key:$k=>val:$v\n";
}
}
?>
If you want to access something specific, you can do it like this:
echo $arr[1]["BBBB"]; // echoes bbbb
Or...
echo $arr[1]["Name"]; // echoes Brent's Medical Center
Your variable is an array filled with associative arrays. So when you're doing your loop and operating on $key, that's not the data, but the index of your parent array.
So simply changing the part of the array you're dealing with will fulfill your original code sample.
foreach ($obj as $key => $value) {
print_r($value);
}
Now each $value is the associative array with the keys Name, date, dealType, etc. So you can get your values directly, e.g. $value['Name'] for the first loop would be "Brent's Medical Center"

Categories