Php array: How can I echo the value of this? [closed] - php

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I am trying to echo a setting on the second level of an array in my wordpress options and having some trouble getting the variable. This code DOES work:
<?php foreach($area['fields'] as $field): ?>
<?php if ($field['id'] == 'show_border')
{echo ' show_border';} ?>
<?php endforeach; ?>
That simply checks if there is a field that asks if you want to show a border and if there is it echos whatever I want, regardless of what the field is set at for actually showing the border or not.
What I am trying to do is to just echo the value that is set in the field "show_border".
Any idea how I would do this?
edit
array(11) {
[0]=>
array(3) {
["name"]=>
string(10) "# of posts"
["id"]=>
string(5) "posts"
["type"]=>
string(4) "text"
}
[1]=>
array(5) {
["name"]=>
string(13) "posts per row"
["id"]=>
string(13) "posts_per_row"
["type"]=>
string(6) "select"
["default"]=>
int(3)
["options"]=>
array(8) {
[1]=>
int(1)
[2]=>
int(2)
[3]=>
int(3)
[4]=>
int(4)
[5]=>
int(5)
[6]=>
int(6)
[7]=>
int(7)
[8]=>
int(8)
}
}
[2]=>
array(5) {
["name"]=>
string(11) "header type"
["id"]=>
string(13) "f_header_type"
["type"]=>
string(6) "select"
["default"]=>
string(2) "h4"
["options"]=>
array(6) {
["h1"]=>
string(2) "h1"
["h2"]=>
string(2) "h2"
["h3"]=>
string(2) "h3"
["h4"]=>
string(2) "h4"
["h5"]=>
string(2) "h5"
["h6"]=>
string(2) "h6"
}
}
[3]=>
array(5) {
["name"]=>
string(5) "icon?"
["id"]=>
string(9) "show_icon"
["type"]=>
string(5) "radio"
["default"]=>
string(3) "yes"
["options"]=>
array(2) {
["yes"]=>
string(3) "yes"
["no"]=>
string(2) "no"
}
}
[4]=>
array(5) {
["name"]=>
string(13) "icon location"
["id"]=>
string(13) "icon_location"
["type"]=>
string(6) "select"
["default"]=>
string(4) "left"
["options"]=>
array(3) {
["left"]=>
string(4) "left"
["right"]=>
string(5) "right"
["top"]=>
string(3) "top"
}
}
[5]=>
array(5) {
["name"]=>
string(10) "show text?"
["id"]=>
string(9) "show_text"
["type"]=>
string(5) "radio"
["default"]=>
string(3) "yes"
["options"]=>
array(2) {
["yes"]=>
string(3) "yes"
["no"]=>
string(2) "no"
}
}
[6]=>
array(5) {
["name"]=>
string(12) "show border?"
["id"]=>
string(11) "show_border"
["type"]=>
string(5) "radio"
["default"]=>
string(2) "no"
["options"]=>
array(3) {
["top"]=>
string(3) "top"
["bot"]=>
string(3) "bot"
["no"]=>
string(2) "no"
}
}
[7]=>
array(5) {
["name"]=>
string(8) "category"
["id"]=>
string(8) "category"
["type"]=>
string(6) "select"
["multiple"]=>
bool(true)
["options"]=>
array(2) {
[4]=>
string(7) "feature"
[3]=>
string(6) "slider"
}
}
[8]=>
array(4) {
["name"]=>
string(14) "custom classes"
["id"]=>
string(14) "custom_classes"
["type"]=>
string(4) "text"
["default"]=>
string(0) ""
}
[9]=>
array(5) {
["name"]=>
string(10) "read more?"
["id"]=>
string(9) "read_more"
["type"]=>
string(5) "radio"
["default"]=>
string(3) "yes"
["options"]=>
array(2) {
["yes"]=>
string(3) "yes"
["no"]=>
string(2) "no"
}
}
[10]=>
array(4) {
["name"]=>
string(14) "read more text"
["id"]=>
string(14) "read_more_text"
["type"]=>
string(4) "text"
["default"]=>
string(16) "Continue reading"
}
}
I am trying to get a value like ["id"]=> "show_border" and echo it's value.

Basically you can't get the form value in PHP which you're going to select from the form which was already loaded on the server-side.
It's like going from the future to the past.
You should use Javascript code + AJAX, instead of PHP, but it'll be much more complex.
Without knowing the full source of the site, it's not possible to tell you in one simple answer.
Also it depends on your development skills.

Related

How do I check a json object for the existence of a key with php server side?

Ok, So I am working with the reddit api using the php sdk wrapper by #jcleblanc ( https://github.com/jcleblanc/reddit-php-sdk )
That information is not necessarily relevant to the question but I want to give you a frame of reference so you understand what I am looking to do.
The api returns what I believe is a mixed json object as a response when I make calls to reddit. The response length varies and is not consistent. The only this that is consistent is if the action was successful, it returns the key pair
["success"]=> bool(true)
I want to basically search whatever is returned by the api for the key "success" and I can simply evaluate for true or false. Can anybody help with some code to do that?
Here is a sample complete return:
object(stdClass)#1171 (2) { ["jquery"]=> array(29) { [0]=> array(4) { [0]=> int(0) [1]=> int(1) [2]=> string(4) "call" [3]=> array(1) { [0]=> string(4) "body" } } [1]=> array(4) { [0]=> int(1) [1]=> int(2) [2]=> string(4) "attr" [3]=> string(4) "find" } [2]=> array(4) { [0]=> int(2) [1]=> int(3) [2]=> string(4) "call" [3]=> array(1) { [0]=> string(7) ".status" } } [3]=> array(4) { [0]=> int(3) [1]=> int(4) [2]=> string(4) "attr" [3]=> string(4) "hide" } [4]=> array(4) { [0]=> int(4) [1]=> int(5) [2]=> string(4) "call" [3]=> array(0) { } } [5]=> array(4) { [0]=> int(5) [1]=> int(6) [2]=> string(4) "attr" [3]=> string(4) "html" } [6]=> array(4) { [0]=> int(6) [1]=> int(7) [2]=> string(4) "call" [3]=> array(1) { [0]=> string(0) "" } } [7]=> array(4) { [0]=> int(7) [1]=> int(8) [2]=> string(4) "attr" [3]=> string(3) "end" } [8]=> array(4) { [0]=> int(8) [1]=> int(9) [2]=> string(4) "call" [3]=> array(0) { } } [9]=> array(4) { [0]=> int(1) [1]=> int(10) [2]=> string(4) "attr" [3]=> string(8) "redirect" } [10]=> array(4) { [0]=> int(10) [1]=> int(11) [2]=> string(4) "call" [3]=> array(1) { [0]=> string(66) "https://www.reddit.com/r/mysubreddit/comments/12hyas2/my_no_link_post/" } } [11]=> array(4) { [0]=> int(1) [1]=> int(12) [2]=> string(4) "attr" [3]=> string(4) "find" } [12]=> array(4) { [0]=> int(12) [1]=> int(13) [2]=> string(4) "call" [3]=> array(1) { [0]=> string(11) "*[name=url]" } } [13]=> array(4) { [0]=> int(13) [1]=> int(14) [2]=> string(4) "attr" [3]=> string(3) "val" } [14]=> array(4) { [0]=> int(14) [1]=> int(15) [2]=> string(4) "call" [3]=> array(1) { [0]=> string(0) "" } } [15]=> array(4) { [0]=> int(15) [1]=> int(16) [2]=> string(4) "attr" [3]=> string(3) "end" } [16]=> array(4) { [0]=> int(16) [1]=> int(17) [2]=> string(4) "call" [3]=> array(0) { } } [17]=> array(4) { [0]=> int(1) [1]=> int(18) [2]=> string(4) "attr" [3]=> string(4) "find" } [18]=> array(4) { [0]=> int(18) [1]=> int(19) [2]=> string(4) "call" [3]=> array(1) { [0]=> string(12) "*[name=text]" } } [19]=> array(4) { [0]=> int(19) [1]=> int(20) [2]=> string(4) "attr" [3]=> string(3) "val" } [20]=> array(4) { [0]=> int(20) [1]=> int(21) [2]=> string(4) "call" [3]=> array(1) { [0]=> string(0) "" } } [21]=> array(4) { [0]=> int(21) [1]=> int(22) [2]=> string(4) "attr" [3]=> string(3) "end" } [22]=> array(4) { [0]=> int(22) [1]=> int(23) [2]=> string(4) "call" [3]=> array(0) { } } [23]=> array(4) { [0]=> int(1) [1]=> int(24) [2]=> string(4) "attr" [3]=> string(4) "find" } [24]=> array(4) { [0]=> int(24) [1]=> int(25) [2]=> string(4) "call" [3]=> array(1) { [0]=> string(13) "*[name=title]" } } [25]=> array(4) { [0]=> int(25) [1]=> int(26) [2]=> string(4) "attr" [3]=> string(3) "val" } [26]=> array(4) { [0]=> int(26) [1]=> int(27) [2]=> string(4) "call" [3]=> array(1) { [0]=> string(1) " " } } [27]=> array(4) { [0]=> int(27) [1]=> int(28) [2]=> string(4) "attr" [3]=> string(3) "end" } [28]=> array(4) { [0]=> int(28) [1]=> int(29) [2]=> string(4) "call" [3]=> array(0) { } } } ["success"]=> bool(true) }
The code you gave mean you were calling json_decode($response). This return an object.
It should be:
$resArray = json_decode($response, true)
Which return an associated array.
Then to use:
if (isset($resArray['success'])) {
//your logic here
}
Your object has two properties: jquery and success.
All you need to get the success property is this:
$success = $your_object->success;
As long as the returned object has a success property, that's all you should need to do, regardless of how many other properties it has, or the size of any of its other properties.
If you aren't sure if the object will have the success property, you can use
$success = !empty($your_object->success);
which will evaluate to true if the property exists and is true, and false otherwise.
You might be able to just throw it into
json_decode
If it complains about the format you can do some direct string manipulation beforehand and then you'll be able to reference it directly as an associative array.
Use var_dump to get familiar with the structure after you get a successful parse.

Accessing form data form JSON object converted to associative array in PHP [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 3 years ago.
I'm sending the HTML FORM data with AJAX to PHP script and I'm converting the data to associative array like this:
$json = json_decode($request->get('json'),true);
so, when I'm dumping the variable I get this array:
array(11) {
[0]=>
array(2) {
["name"]=>
string(21) "form[pickupDate][day]"
["value"]=>
string(1) "4"
}
[1]=>
array(2) {
["name"]=>
string(23) "form[pickupDate][month]"
["value"]=>
string(1) "1"
}
[2]=>
array(2) {
["name"]=>
string(22) "form[pickupDate][year]"
["value"]=>
string(4) "2016"
}
[3]=>
array(2) {
["name"]=>
string(22) "form[pickupTime][hour]"
["value"]=>
string(1) "0"
}
[4]=>
array(2) {
["name"]=>
string(21) "form[returnDate][day]"
["value"]=>
string(1) "1"
}
[5]=>
array(2) {
["name"]=>
string(23) "form[returnDate][month]"
["value"]=>
string(1) "1"
}
[6]=>
array(2) {
["name"]=>
string(22) "form[returnDate][year]"
["value"]=>
string(4) "2016"
}
[7]=>
array(2) {
["name"]=>
string(22) "form[returnTime][hour]"
["value"]=>
string(1) "0"
}
[8]=>
array(2) {
["name"]=>
string(19) "form[pickupAddress]"
["value"]=>
string(0) ""
}
[9]=>
array(2) {
["name"]=>
string(12) "form[agency]"
["value"]=>
string(1) "1"
}
[10]=>
array(2) {
["name"]=>
string(12) "form[_token]"
["value"]=>
string(43) "9dh6ghpMv5K9LUdSLvh6y2NOzqTzUrOfVriL8C63Ybs"
}
}
how can I get the value of form[pickupDate][day] without loop?
I was able to do it like this:
echo array_column($json, null, 'name')['form[pickupDate][day]']['value'];

PHP - multi-d array values changing between foreach loops

New PHP user left scratching my head. This function contains two foreach loops nested in if/else the page is index.php. On index.php, the code works fine, and the page shows the last four items in the array.
However, if the page is NOT index.php, the array seems to have been affected by code in between the first and second foreach loop, and only shows as a 1D array with the first-level keys as integers. How do I fix this? I've gone through virtually every PHP documentation I can find with no success.
function list_products_html($products) {
//If page is INDEX.PHP, show only last 4 products in reverse order with PayPal drop-down and Add To Cart
if ($_SERVER['PHP_SELF'] == "index.php") {
$products_reverse = array_reverse($products, true);
$products_new = array_slice($products_reverse, 0, 4, true);
foreach ($products_new as $product_new_id => $product_new) {
echo list_product_html($products_new);
echo generate_paypal_form($products_new);
echo '</li>';
}
//Else, show all products
} else {
foreach ($products as $product_id => $product) {
echo list_product_html($products);
echo generate_paypal_form($products);
echo '</li>';
}
}
}
In the first foreach loop, the var_dump shows my array as the following (which is correct):
array(8) { [101]=> array(5) { ["name"]=> string(15) "Logo Shirt, Red" ["img"]=> string(24) "img/shirts/shirt-101.jpg" ["price"]=> int(18) ["paypal"]=> string(13) "BJDE2BMPHMQ7Q" ["sizes"]=> array(4) { [0]=> string(5) "Small" [1]=> string(6) "Medium" [2]=> string(5) "Large" [3]=> string(7) "X-Large" } } [102]=> array(5) { ["name"]=> string(26) "Mike the Frog Shirt, Black" ["img"]=> string(24) "img/shirts/shirt-102.jpg" ["price"]=> int(20) ["paypal"]=> string(13) "2H6VLSYHY3QC8" ["sizes"]=> array(4) { [0]=> string(5) "Small" [1]=> string(6) "Medium" [2]=> string(5) "Large" [3]=> string(7) "X-Large" } } [103]=> array(5) { ["name"]=> string(25) "Mike the Frog Shirt, Blue" ["img"]=> string(24) "img/shirts/shirt-103.jpg" ["price"]=> int(20) ["paypal"]=> string(13) "MDP23L74U3F4L" ["sizes"]=> array(4) { [0]=> string(5) "Small" [1]=> string(6) "Medium" [2]=> string(5) "Large" [3]=> string(7) "X-Large" } } [104]=> array(5) { ["name"]=> string(17) "Logo Shirt, Green" ["img"]=> string(24) "img/shirts/shirt-104.jpg" ["price"]=> int(18) ["paypal"]=> string(13) "Q39L2XSPANB3Y" ["sizes"]=> array(4) { [0]=> string(5) "Small" [1]=> string(6) "Medium" [2]=> string(5) "Large" [3]=> string(7) "X-Large" } } [105]=> array(5) { ["name"]=> string(27) "Mike the Frog Shirt, Yellow" ["img"]=> string(24) "img/shirts/shirt-105.jpg" ["price"]=> int(25) ["paypal"]=> string(13) "7LVQ78CEPSKVN" ["sizes"]=> array(4) { [0]=> string(5) "Small" [1]=> string(6) "Medium" [2]=> string(5) "Large" [3]=> string(7) "X-Large" } } [106]=> array(5) { ["name"]=> string(16) "Logo Shirt, Gray" ["img"]=> string(24) "img/shirts/shirt-106.jpg" ["price"]=> int(20) ["paypal"]=> string(13) "G9A9UEVWCTDQN" ["sizes"]=> array(4) { [0]=> string(5) "Small" [1]=> string(6) "Medium" [2]=> string(5) "Large" [3]=> string(7) "X-Large" } } [107]=> array(5) { ["name"]=> string(21) "Logo Shirt, Turquoise" ["img"]=> string(24) "img/shirts/shirt-107.jpg" ["price"]=> int(20) ["paypal"]=> string(13) "NNERMRTJSTG6Q" ["sizes"]=> array(4) { [0]=> string(5) "Small" [1]=> string(6) "Medium" [2]=> string(5) "Large" [3]=> string(7) "X-Large" } } [108]=> array(5) { ["name"]=> string(18) "Logo Shirt, Orange" ["img"]=> string(24) "img/shirts/shirt-108.jpg" ["price"]=> int(25) ["paypal"]=> string(13)
In the SECOND foreach loop, I am getting the
"Warning: Invalid argument supplied for foreach()"
error, because var_dump for the same $products array shows the following:
int(101) int(102) int(103) int(104) int(105) int(106) int(107)
int(108)

How to read var_dump in order to know how to load its variable contents in to an array with a foreach?

Here is a sample var_dump. Now, how do I know how to construct a foreach from it to load any particular word or fragment in to an array?
object(stdClass)#1 (2)
{
["noun"]=>
object(stdClass)#2 (1)
{
["syn"]=> array(24)
{
[0]=> string(12) "domestic dog"
[1]=> string(16) "Canis familiaris"
[2]=> string(5) "frump"
[3]=> string(3) "cad"
[4]=> string(7) "bounder"
[5]=> string(10) "blackguard"
[6]=> string(5) "hound"
[7]=> string(4) "heel"
[8]=> string(5) "frank"
[9]=> string(11) "frankfurter"
[10]=> string(6) "hotdog"
[11]=> string(7) "hot dog"
[12]=> string(6) "wiener"
[13]=> string(11) "wienerwurst"
[14]=> string(6) "weenie"
[15]=> string(4) "pawl"
[16]=> string(6) "detent"
[17]=> string(5) "click"
[18]=> string(7) "andiron"
[19]=> string(7) "firedog"
[20]=> string(8) "dog-iron"
[21]=> string(8) "blighter"
[22]=> string(5) "canid"
[23]=> string(6) "canine"
[24]=> string(5) "catch"
}
}
}
Before we can decipher it, we have to format it.
Tobias Kun's answer shows a very good way to format the var_dump output so you can read it.
object(stdClass)#1 (2)
{
["noun"]=>
object(stdClass)#2 (1)
{
["syn"]=> array(24)
{
[0]=> string(12) "domestic dog"
[1]=> string(16) "Canis familiaris"
[2]=> string(5) "frump"
[3]=> string(3) "cad"
[4]=> string(7) "bounder"
[5]=> string(10) "blackguard"
[6]=> string(5) "hound"
[7]=> string(4) "heel"
[8]=> string(5) "frank"
[9]=> string(11) "frankfurter"
[10]=> string(6) "hotdog"
[11]=> string(7) "hot dog"
[12]=> string(6) "wiener"
[13]=> string(11) "wienerwurst"
[14]=> string(6) "weenie"
[15]=> string(4) "pawl"
[16]=> string(6) "detent"
[17]=> string(5) "click"
[18]=> string(7) "andiron"
[19]=> string(7) "firedog"
[20]=> string(8) "dog-iron"
[21]=> string(8) "blighter"
[22]=> string(5) "canid"
[23]=> string(6) "canine"
[24]=> string(5) "catch"
}
}
}
You have a stdClass object with a property called ,"noun". noun is an abject with a property called "syn", which is an array of strings.
Suppose we call the object $object. Then we can access the array like:
echo $object->noun->syn[23];
which gives us canine. So a loop might look like:
foreach($data->noun->syn as $value) {
echo $value . "<br>\n";
}
First of all you should really increase the quality of your questions. The code is not formatted at all.
If you use echo "<pre>" . print_r($your_data_object_or_array,1) . "</pre>" your data will be formatted fine.
If i understand you right this should help you:
foreach($data['noun']['syn'] as $value) {
//with this you loop through all your words in "syn" e.g. domestic, "Canis familiaris etc."
echo $value . "<br>";
}
//Ouput:
domestic
Canis familiaris
frump
cad
etc ...

Retrieve value from an Object / Multi-Dimensional Array Received from a Web Service

I am calling a web service, and receive this response which I can see using var_dump. How do I get the Name value? Also, what structure is this?
object(DescribeEntityTypeResponse)#2 (1) {
["DescribeEntityTypeResult"]=>
object(DescribeEntityTypeResult)#6 (1) {
["EntityTypes"]=>
object(ArrayOfEntityType)#7 (1) {
["EntityType"]=>
array(5) {
[0]=>
object(EntityType)#8 (3) {
["ID"]=>
int(0)
["Name"]=>
string(7) "Contact"
["Type"]=>
string(4) "Base"
}
[1]=>
object(EntityType)#9 (3) {
["ID"]=>
int(0)
["Name"]=>
string(7) "Company"
["Type"]=>
string(4) "Base"
}
[2]=>
object(EntityType)#10 (3) {
["ID"]=>
int(0)
["Name"]=>
string(8) "Prospect"
["Type"]=>
string(4) "Base"
}
[3]=>
object(EntityType)#11 (3) {
["ID"]=>
int(0)
["Name"]=>
string(7) "Visitor"
["Type"]=>
string(4) "Base"
}
[4]=>
object(EntityType)#12 (3) {
["ID"]=>
int(0)
["Name"]=>
string(17) "ProcessedActivity"
["Type"]=>
string(4) "Base"
}
}
}
}
}
I think this should work:
foreach($result->DescribeEntityTypeResult->EntityTypes->EntityType as $entityType) {
$name = $entityType['Name'];
}
However the objects may have methods() to access these properties.

Categories