Get element of the object in PHP array [duplicate] - php

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 2 years ago.
So I got this array with associative object in PHP and I couldnot figure out how to get specific element
here is an array:
extra_fields => [
{"id":"1","value":"1055"},
{"id":"2","value":"Link"},
{"id":"3","value":"Name"}
]
I tried like this but it doesn't work
extra_fields[0]["value"]) and extra_fields[0]->value
Please help.
UPDATE:
full output code:
stdClass Object
(
[id] => 723
[title] => XXXXXXX
[alias] => XXXXXXX
[catid] => 50
[published] => 1
[introtext] =>
[fulltext] =>
[video] =>
[gallery] =>
[extra_fields] => [
{"id":"1","value":"1055"},
{"id":"2","value":"Link"},
{"id":"3","value":"Name"}
]
)
this is an $item coming out of Joomla CMS K2 plugin when I use print_r() command
I can access normal stuff like this $item->title and get XXXXXXX for my value, but could not figure out how to get items from extra_fields

as others mentioned, this doesn't seem valid PHP data, however if you would convert array of objects to multidimensional array, then do it this way:-
<?php
// new var that holds the arrays
$multiArray = [];
foreach ($extra_fields as $field){
$multiArray[] = (array) $field;
}
// now this var has the exact data that you want...
print_r($mulitArray);
Hope this helps.

The solution to my problem was pretty simple, I used json_decode() function to convert it too array
$someArray = json_decode($item->extra_fields, true);
print_r($someArray[0]["value"]);
Thank you all for your help

Related

Collect all values from nested array row of specific key [duplicate]

This question already has answers here:
How to get an array of specific "key" in multidimensional array without looping [duplicate]
(4 answers)
Closed 1 year ago.
Need to create a list that consists of all values stored in the array row of a specific key (product_id). Currently, doing a print_r of my $bestsellers variable produces the following array:
Array
(
[0] => stdClass Object
(
[product_id] => 178
[order_item_qty] => 9
)
[1] => stdClass Object
(
[product_id] => 233
[order_item_qty] => 4
)
[2] => stdClass Object
(
[product_id] => 179
[order_item_qty] => 1
)
)
Other SO answers led me to try:
$ids = array_column($bestsellers, 'product_id');
...but that produced an empty array, I guess because the row I’m trying to grab is nested in there? With that in mind I tried
foreach($bestsellers as $bestseller) {
$ids = array_column($bestsellers, 'product_id');
}
...which produced no result at all.
Hopefully someone can help clue me in as to where I’m going wrong. Thanks!
The nested values are objects, not arrays (can't you see stdClass Object in the output?). array_column is for 2-dimensional arrays. You need to access the properties using object syntax.
$ids = array_map(function($x) { return $x->product_id; }, $bestsellers);
For future reference, array_column will work for this in PHP 7, so you must be using PHP 5.
For PHP 7, your code
$ids = array_column($bestsellers, 'product_id');
will do what you want it to.
See the difference here on 3v4l.org.

get value from array with std object (php) [duplicate]

This question already has answers here:
stdClass object array inside of another array
(3 answers)
Closed 5 years ago.
my code in PHP :
$stuff = array($this->complaint_model->get_reply($id)->result());
print_r($stuff);
result :
Array (
[0] => Array (
[0] => stdClass Object (
[id_customer] => 21
[id_complaint] => 2
[id] => 4
[nama_customer] => Muhammad Bima Zehansyah
[from] => Admin
[balasan] => coba update
)
)
)
my question is , how to get value [nama_customer] ? thx before guys
Try this
$stuff = array($this->complaint_model->get_reply($id)->result());
echo $stuffVal = $stuff[0][0]->nama_customer;
Get the value like this
$stuff[0][0]->nama_customer
Here you have multidimensional array object that's why you need to first Travers two array like $stuff[0][0] and then the object like $stuff[0][0]->nama_customer
Actually you do not need to put the result into additional array and it would be wise to check if the result is not empty.
So you just take a first item from your result (which is an object) and call the parameter nama_customer
$stuff = $this->complaint_model->get_reply($id)->result();
if (!empty($stuff[0]))
echo $stuff[0]->nama_customer;

PHP, file_get_contents, for loop, str_replace

I am quite new with PHP and I am trying to read something form an API.
At the moment I use
$homepage = file_get_contents('http://www.site.com');
echo $homepage
This returns something which looks like this:
Array
(
[0] => Array
(
[name] => myname
[user_id] => 31232
)
[1] => Array
(
[name] => anothername
[user_id] => 23534
)
)
So here is what I want: I only want to read the [name] => x and leave the rest, so I tried a for loop within str_replace, but all I got were errors.
I hope someone is able to help me
Edit:
I just saw I could set is as a json text too, it returns something like
[{"name":"myname","user_id":"31232"},{"name":"anothername","user_id":"23534"}]
Edit2: Thank you Tuga, that was exactly what I was searching for :) I can't upvote, since my reputaion is below 15, is there another way for me to show your answer helped?
$array = json_decode($homepage); will return an array, then you can loop the array containing objects and use 'name' attribute :
foreach ($array as $obj) echo $obj->name;

PHP array with multiple delimiters [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JSON Parsing with PHP
I'm hoping for a little assistance on this. I trying to take what's in my $data variable:
[{"Product":"Internal",
"Rank":"1",
"Number":"1234"},
{"Product":"External",
"Rank":"1",
"Number":"5678"}]
and turn it into an associative array that is something like this:
Product[0] => Internal,
Rank[0] => 1,
Number[0] => 1234,
Product[1] => External,
Rank[1] => 1,
Number[1] => 5678
The closest I've been able to get is using the following code:
$del='/[{: ,}]/';
$data = preg_split($del,$data);
print_r($data);
Which gives me something like this:
Array ( [0] => [ [1] => "Product" [2] => "1 [3] => 1" [4] => "Rank"
[5] => "1"
Any suggestions would be greatly appreciated. Thanks for your time!
If you know that $data is in JSON format, you could just do this:
$data = json_decode($data);
Then you want to iterate through the array of objects and transmogrify them:
$output = array();
foreach($data as $index=>$object) {
foreach($object as $name=>$value) {
$output[$name][$index] = $value;
}
}
Firstly, use json_decode($data,TRUE), this will make your life easier, giving you nice array to transform in what you want.

Extract values from php array [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Able to see a variable in print_r()'s output, but not sure how to access it in code
I am using SOAP to get data from the server and in response i am getting a php array like this
Array
(
[BookResult] => stdClass Object
(
[PNR] => 5WPODU
[BookingId] => 31149
[Status] => stdClass Object
(
[StatusCode] => 03
[Description] => Fare is not available at the time of booking
[Category] => BK
)
[SSRDenied] => N
[ProdType] => Flight
)
)
All i want to know is how can i extract "PNR" and "StatusCode" value in separate variables so that i can store them in database.
Tried this not working
$p = (object) $array;
echo $p->StatusCode;
Try this:
$PNR = $array["BookResult"]->PNR;
$StatusCode= $array["BookResult"]->Status->StatusCode;
$array is an array. So first dive is $array['BookResult'].
BookResult is stdClass instance so next goes $array['BookResult']->Status (get object's property).
Status is also stdClass instance so get it's property: $array['BookResult']->Status->StatusCode
var_dump($array['BookResult']->PNR);
var_dump($array['BookResult']->Status->StatusCode);
Assuming results are being stored in $array
echo $array['BookResult']->Status->StatusCode;
echo $array['BookResult']->PNR;

Categories