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;
Related
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
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.
This question already has answers here:
Is there a function to extract a 'column' from an array in PHP?
(15 answers)
Closed 5 years ago.
I've the following array:
Array
(
[0] => Array
(
[ID] => 1
[more_user_data] => More
)
[1] => Array
(
[ID] => 2
[more_user_data] => More
)
)
Now I want to have a comma separated list of the IDs to use them in an own array. To get something like this:
array(1,2)
How could I only extract the IDs from the second array?
Use array_column() function like:
$arr = array_column($array, 'ID');
Working Example
This question already has answers here:
How to reindex an array?
(6 answers)
Closed 7 years ago.
I apologize in advance for this question - I'm sure it's been answered before, (hell, I've done it before), I just can't find anything in the search and my brain is drawing a blank...
I have an array in PHP where print_r outputs:
Array
(
[0] => VALUE
[1] => VALUE
[2] => VALUE
[3] => VALUE
[4] => VALUE
)
I use unset($_SESSION['item'][$lineID]); to unset a particular line from the array, and I'm left with:
Array
(
[0] => VALUE
[1] => VALUE
[3] => VALUE
[4] => VALUE
)
Element [2] has been removed, so the resulting array is no longer numerically in order
(e.g. - [0][1][3][4] instead of [0][1][2][3] ).
What PHP command do I run on the resulting array to "take out blanks" and reformat it so that it is numerically in order from 1 to n
To reset array keys after unset()
$array = array_values($array);
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;