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'));
Related
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>';
How can I interpret this JSON decoded array? It seems like more of a complex array than I'm accustomed to dealing with so any help would be appreciated.
Thanks.
array(1){
[0]=> object(stdClass)#1 (11){
["id"]=> string(5) "72324"
["txid"]=> string(64) "**bitcoin_tx_id**"
["from"]=> string(34) "**bitcoin_address**"
["to"]=> string(20) "email#somewhere.com"
["amount"]=> int(10000000)
["amount_sent"]=> int(0)
["note"]=> string(0) ""
["time"]=> float(1379742767000)
["to_addr"]=> string(34) "**bitcoin_address**"
["read"]=> string(1) "1"
["balance"]=> string(10) "0.10000000"
}
}
It is returning an array of objects, it looks like you're just dumping it to stdout. Toss the output from json_decode() inside a variable and you may access it like this:
$decoded = json_decode($data);
foreach($decoded as $obj) {
echo "ID: " . $obj->id . ', ';
echo "TXID: " . $obj->txid . ', ';
echo "From: " . $obj->from . ', ';
echo "To: " . $obj->to. ', ';
// ...
echo "<br>";
}
If you want json_decode() to return an associative array (which most people are used to), simply set the second parameter to true.
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.
this must be very simple but I couldn't make it work.. PHP noob :P
I have this array "$e_cats" and when I do var_dump($e_cats); the result is this:
array(3) { [0]=> string(3) "192" [1]=> string(3) "190" [2]=> string(3) "191" }
What I want is to add "-" to every value inside, so "-192", "-190", and "-191". Here is my code:
foreach ($e_cats as $cat) {
$cat = '-' .$cat;
}
but when I do print_r($cat) the result is: -191 (not all values). What did I do wrong?
Thanks in advance
foreach($e_cats as $i => $cat) {
$e_cats[$i] = '-' . $cat;
}
You were close!
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"