Getting Value from Nested Array -- PHP Wordpress -- - php

I am using get_post_meta like below:
$job_owner = get_post_meta($post->ID, 'assignedUsers', true);
That returns the following:
(
[total] => 1
[data] => Array
(
[0] => stdClass Object
(
[id] => 440968
[firstName] => John
[lastName] => Doe
[email] => john#website.com
)
)
)
I am trying to grab the values from the object but catch an error each time I simply try and use echo $job_owner. Error is - Object of class stdClass could not be converted to string
I have tried to use:
$array = json_decode(json_encode($job_owner), true);
Which returns the arrays:
Array
(
[total] => 1
[data] => Array
(
[0] => Array
(
[id] => 440968
[firstName] => Megan
[lastName] => Collins
[email] => megan#bridgeviewit.com
)
)
)
But I cannot seem to get anything to return using echo $array[0]->id etc...
My ideal scenario is to use the array values as variables to use throughout the theme.

So with echo $array[0]->id are trying to get the 0th element of that array which is total. So echo $array[0]->id should fail but echo $array[0] should return 1. If you change your request to $array[data][0]->id or $array[1][0]->id that should get you the value you are looking for - the id of the first element in the data bit.
$array[data][0]->id

Related

PHP Edit Value of stdClass Object

I have an array called $list that returns multiple values, I'm looping through this and displaying these values. I need to edit one of the original values.
Here's the result of;
print("<pre>".print_r($list,true)."</pre>");
Array
(
[0] => stdClass Object
(
[name] => Home
[link] => /example/
)
[1] => stdClass Object
(
[name] => Items
[link] => /example/locations/items <-- I want to edit this value
)
[2] => stdClass Object
(
[name] => Paris
[link] => /example/locations/paris
)
)
The code below;
foreach ($list as $key => $item) {
echo $item->link;
}
Produces;
/example/
/example/locations/items
/example/locations/paris
I need to edit the link value IF the name key is Items. I essentially want to remove the last URL parameter so the output looks like this;
/example/
/example/locations
/example/locations/paris
Note that it will not always be position [1] in the array, but the name value will always be Items.
How can I achieve this?
You could use array_walk, passing it a function which checked for the name key being Items and if so returning the dirname of the link key:
array_walk($list, function (&$v) { if ($v->name == 'Items') $v->link = dirname($v->link); });
Output:
Array
(
[0] => stdClass Object
(
[name] => Home
[link] => /example/
)
[1] => stdClass Object
(
[name] => Items
[link] => /example/locations
)
[2] => stdClass Object
(
[name] => Paris
[link] => /example/locations/paris
)
)
Demo on 3v4l.org

Php unserialized a serialized json string

After a http post in c# to php am getting an output of the form
in the code i have
public function actionSubmitInspection(){
$data = $_POST;
return (array)$data["check_comments"];
}
now am getting a result of the form
[
"[{\"id\":26,\"comment\":\"89oiu\"},{\"id\":27,\"comment\":\"comment 2\"}]"
]
as from my try typecasting array doesnt creating the array, How can i convert the serialized string to an array or an object.
use json_decode function.
public function actionSubmitInspection(){
$data = $_POST;
// replace it
//return (array)$data["check_comments"];
return json_decode($data["check_comments"]);
}
Out put will be array of objects.
Array
(
[0] => stdClass Object
(
[id] => 26
[comment] => 89oiu
)
[1] => stdClass Object
(
[id] => 27
[comment] => comment 2
)
)
as from my try typecasting array doesnt creating the array
Yes, it creates an array but the array it creates contains the JSON text.
You need to parse the JSON in order to restore the data structures it encodes. PHP provides the function json_decode() for this purpose. I recommend you pass TRUE as the second argument to json_decode() to get back arrays (otherwise it creates stdClass objects that are just arrays with a fancy syntax and limited options for processing).
// Assuming the value of $data['check_comments'] is:
// "[{\"id\":26,\"comment\":\"89oiu\"},{\"id\":27,\"comment\":\"comment 2\"}]"
$output = json_decode($data['check_comments']);
print_r($output);
The output:
Array
(
[0] => Array
(
[id] => 26
[comment] => 89oiu
)
[1] => Array
(
[id] => 27
[comment] => comment 2
)
)
You should use json_decode($data["check_comments"]) the output will be an array of stdClass objects:
Array
(
[0] => stdClass Object
(
[id] => 26
[comment] => 89oiu
)
[1] => stdClass Object
(
[id] => 27
[comment] => comment 2
)
)
or passing true on second param, json_decode($data["check_comments"], true) and the output will be an array of arrays:
Array
(
[0] => Array
(
[id] => 26
[comment] => 89oiu
)
[1] => Array
(
[id] => 27
[comment] => comment 2
)
)

How can I extract a PHP string from this sub-array?

The array I am looking at is this:
Array
(
[0] => Array
(
[0] => Bigcommerce\Api\Resources\ProductCustomField Object
(
[ignoreOnCreate:protected] => Array
(
[0] => id
[1] => product_id
)
[ignoreOnUpdate:protected] => Array
(
[0] => id
[1] => product_id
)
[fields:protected] => stdClass Object
(
[id] => 17
[product_id] => 3232
[name] => Artist
[text] => Test
)
[id:protected] => 17
[ignoreIfZero:protected] => Array
(
)
)
)
)
I want to check to see if 'Artist' exists in a php conditional statement. But I don't know how to turn 'Artist' into a string.
UPDATED:
I did not find understand how to extract that value into a string, but I got what I was looking for using the method related to the bigcommerce api:
$customs = Bigcommerce::getProductCustomFields($product->id);
foreach($customs as $custom) {
if($custom->name == 'Artist'): // do something
endif;
}
Ok, looking at the source, it seems you should be able to use the magic __get method. Try
$array[0][0]->name == 'Artist'
The value of the custom field would be stored in the "text" resource for that custom field.
See the following link where you can see the 4 properties of a custom field.
https://developer.bigcommerce.com/api/stores/v2/products/custom_fields

How array data stored in a session

I have a challenge in getting back the array i stored in a session.
I stored the array in a array like this
$downlines = '2,3,4,5,6,7,8';
$_SESSION['downline'] = $afrisoft->dbarray("SELECT username, email FROM users WHERE id IN $downlines")
When i print_r the session i get this
Array (
[0] => Array (
[username] => mcbel
[email] => firstmail#gmail.com
)
[1] => Array (
[username] => bimibola
[email] => secondmail#yahoo.com
)
[2] => Array (
[username] => shadie
[email] => thirdmail#gmail.com
)
[3] => Array (
[username] => Hifee
[email] => ife#ife.net
)
)
What i intend to achieve is to get the data stored in ['usernmae'] and ['email'], however when i try to print_r($_SESSION['downline']['username']) and print_r($_SESSION['downline']['email']) it returns no values.
I'll appreciate any help i can get on this.
You have an array that is returned by your function. you can access the first element of that array by $_SESSION['downline'][0]['username']
you can access the full results by traversing through $_SESSION['downline'] by doing
foreach ($_SESSION['downline'] as $item) {
echo $item['username']
}
<?php
foreach($_SESSION['downline'] as $userDetails){
echo $userDetails['username'];
}

How to get specific data from array

I have data like this:
Array
(
[data] => Array
(
[0] => Array
(
[name] => xxxxxxxxxxxx
[category] => yyyyyyyy
[id] => 12345666666
[data] => ABCDE
)
[1] => Array
(
[name] => ZZZZZZZZZZZ
[category] => JJJJJJ
[id] => 88888888888888
[data] => ABCDEHIJK
)
)
)
I need to get the data from the first array only. I don't want to use a loop. I have tried this:
$name = $data['data'][0]['name'];
but that doesn't get the name. Does anyone know how I can get the specific name only from the first array?
EDIT** my code:
$movie_details_tmdb = $tmdb->searchMovie($search);
$movie_details_tmdb_results = json_decode(json_encode($movie_details_tmdb), true);
$id = $movie_details_tmdb_results['results'][0]['id'];
it's in the same format as the code above.
maybe you have problems with json?
(PHP 5 >= 5.2.0, PECL json >= 1.2.0)
The better way to convert object to array:
$arr = (array) $obj;

Categories