Selecting data from var_dump? - php

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.

Related

How to get first value of array php

How can i get the first value of this array?
I want get jpeg value from each array, but it just returns "hh".
This is my code:
$image_output = wp_get_attachment_image_src( $id );
var_dump($image_output);
foreach ($image_output as $value) {
echo $value;
}
This is my array
array(4) {
[0]=>
string(98) "localhost/wp-content/uploads/2018/06/11.jpeg"
[1]=>
int(170)
[2]=>
int(120)
[3]=>
bool(true)
}
array(4) {
[0]=>
string(63) "localhost/wp-content/uploads/2018/06/10.jpeg"
[1]=>
int(170)
[2]=>
int(120)
[3]=>
bool(true)
}
Where is my code wrong?
If I understand it correct it's a multidimensional array with the link in item 0 of each array.
Echo implode("<br>\n", array_column($image_output, 0));
This should output the links one on each line without looping

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

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>';

create ul and li using a multidimensional array in php

I have the following array:
$tree_array
When I do a var_dump, I get:
array(6) {
[0]=> string(23) "$100,000 Cash Flow 2013"
[1]=> array(6) {
[0]=> string(1) "2" ["Goal_ID"]=> string(1) "2"
[1]=> string(13) "Sell Iron Oak" ["Opportunity"]=> string(13) "Sell Iron Oak"
[2]=> string(2) "10" ["OID"]=> string(2) "10"
}
[2]=> array(2) {
[0]=> string(32) "ask her if she would like to buy" ["Activity"]=> string(32) "ask her if she would like to buy"
}
[3]=> array(6) {
[0]=> string(1) "2" ["Goal_ID"]=> string(1) "2"
[1]=> string(8) "Sell Car" ["Opportunity"]=> string(8) "Sell Car"
[2]=> string(2) "11" ["OID"]=> string(2) "11"
}
[4]=> array(2) {
[0]=> string(52) "Call Roy back to see if he would like to purchase it" ["Activity"]=> string(52) "Call Roy back to see if he would like to purchase it"
}
[5]=> array(1) {
["tot_opp"]=> NULL
}
}
My end goal is to create unordered lists and lists (ul, li) with this data. There will be more data added to the array as the database gets updated, so it will keep growing. My goal is to loop through the array and have it create the following code and be able to keep creating lists as the data grows. I am new to php and not sure how to accomplish this.
<ul>
<li>$100,000 Cash Flow 2013</li>
<ul>
<li>Sell Iron Oak</li>
<ul>
<li>ask her if she would like to buy</li>
</ul>
<ul>
<li>Sell Car</li>
</ul>etc...
Any help will be greatly appreciated! Thank you in advance!
You need a recursive function for that, not a loop. This way it will handle any depth of your source array.
function make_list($arr)
{
$return = '<ul>';
foreach ($arr as $item)
{
$return .= '<li>' . (is_array($item) ? make_list($item) : $item) . '</li>';
}
$return .= '</ul>';
return $return;
}
echo make_list($source_array);
Seems like a simple enough recursion to me:
function arrayToList($in) {
echo "<ul>";
foreach($in as $v) {
if( is_array($v)) arrayToList($v);
else echo '<li>' . $v . '</li>';
}
echo "</ul>";
}
It looks like you have some duplicate values up there. Are you using mysql_fetch_array? You should be using mysql_fetch_assoc or mysql_fetch_row depending on whether you need an associative or indexed array.

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"

PHP: help with array structure

My last question helped me get data from a form into an array. The array is fairly different in structure to anything I've used before. I have multiple records with a textbox and checkbox each. Here is the form data:
array(4) {
["product"]=> array(4) {
[0]=> string(5) "Dummy"
[1]=> string(7) "Dummy 2"
[2]=> string(7) "Dummy 3"
[3]=> string(7) "Dummy 4"
}
["tags"]=> array(4) {
[0]=> string(25) "#enter, #product, #hastag"
[1]=> string(0) ""
[2]=> string(25) "#enter, #product, #hastag"
[3]=> string(25) "#enter, #product, #hastag"
}
["chk"]=> array(2) {
[0]=> string(2) "on"
[2]=> string(2) "on"
}
["submit"]=> string(5) "tweet"
}
I want to get the data from this array into a form such as (only if chk = "on"):
tweet[0]["product"] = "dummy"
tweet[0]["tag"] = "hash tag list here"
tweet[1]["product"] = "dummy3"
tweet[1]["tag"] = "hash tag list here"
Any help most appreciated! :)
First I'd recommend using 1 instead of on. You should try to use numerical values whenever possible as they require less processing. I think you need to readjust your HTML, so you don't do any processing on the PHP side at all...
<input type="checkbox" name="tweet[(YOUR_TWEET_ID)][product]" value="PRODUCT_NAME"/>
<input type="text" name="tweet[(YOUR_TWEET_ID)][tags]" value=""/>
This will cause the form to $_POST exactly how you want it without any additional code.
update
foreach ($_POST['tweet'] as $tweetId => $value) {
//again, it'd be a good idea to use a numerical value here
if (strlen($value['product']) > 0) {
//this tweet was checked, lets do with it what we need
//here's how you'd get the tags
echo $_POST['tweet'][$tweetId]['tags'];
}
}
$finalArray = array();
foreach($array['product'] as $key => $name){
if(!empty($array['chk'][$key])){
$finalArray[] = array(
"product" => $name,
"tag" => $array['tags'][$key]);
};
}
Pretty simple:
$i=0;
foreach ($array['chk'] as $key => $val) {
if ($val == "on") {
$new_array[$i]['product'] = $array['product'][$key];
$new_array[$i++]['tags'] = $array['tags'][$key];
}
}
print_r($new_array);
Should do it, there are other ways, this is just one of them.
foreach ($records['chk'] as $id => $temp) {
$tweet[$id]['product'] = $records['product'][$id];
$tweet[$id]['tag'] = $records['tags'][$id];
}

Categories