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.
Related
This question already has answers here:
Change the array KEY to a value from sub array
(7 answers)
How can i get sub array value to main array key? [Php] [duplicate]
(2 answers)
Multidimensional indexed array to associative array depending on column value
(2 answers)
Closed 10 months ago.
I have a function that work good in php 7 but in php 8.0.11 has warning
$orders->result = array_map('reset', $orders->result);
E_WARNING: reset(): Argument #1 ($array) must be passed by reference, value given in
Of course, I can use this code instead that but maybe slower than array_map
foreach ($orders->result as $item)
$result[$item[0]['order_id']] = $item[0];
Edit:
before foreach or array_map the output like this
Array
(
[0] => Array
(
[order_id] => 41111909
[shop_id] => 34277
[user_id] => 42363
[status_id] => 4
)
)
after use foreach output like this
Array
(
[41111909] => Array
(
[order_id] => 41111909
[shop_id] => 34277
[user_id] => 42363
[status_id] => 4
)
)
How to solve it ?
The simplest replacement is to make an arrow function (single-expression closure) to get the element for you.
If the arrays are plain lists, so that you're always getting element 0, that's as simple as:
$orders->result = array_map(fn($item) => $item[0], $orders->result);
Another alternative in this case is array_column:
$orders->result = array_colum($orders->result, 0);
If you have other keys, you could use either reset or array_key_first in the callback instead:
$orders->result = array_map(fn($item) => $item[array_key_first($item)], $orders->result);
(I suspect array_key_first will be slightly more efficient, since it doesn't need to manipulate the internal array pointer.)
It's not entirely clear what you're trying to do, but it seems like you are trying to get the first value from nested arrays. You should be able to use a function:
$orders->result = array_map(function($v) { return reset($v); }, $orders->result);
You may also try using current if it does what you need as it doesn't take a reference and the array pointer should already be reset by array_map:
$orders->result = array_map('current', $orders->result);
After the edit the code in question is not re-indexing the array as you show in the before and after. To do that is simple:
$orders->result = array_column($orders->result, null, 'order_id');
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:
Implode a column of values from a two dimensional array [duplicate]
(3 answers)
Closed 7 months ago.
when I print_r $output I get the following
Array ( [0] => stdClass Object ( [id] => foo ) [1] => stdClass Object ( [id] => bar ) [2] => stdClass Object ( [id] => foobar ))
Im trying to do something which I though would be simple and convert the $output into
foo, bar, foobar
I have tried using for each statements such as below
foreach($array as $row){
$new_array[$row['nid']] = $row['nid'];
}
print_r($new_array);
but I just get invalid php.
I have tried ausing arraywalk and other examples here but not getting anything to work.
So how do I convert the following Array into a simple list of IDs?
Thanks for any help
Except for the comma separated list, what you have should work in theory, but the code doesn't at all match your array. You state $object not $array and the property is id not nid.
However, just extract the id column and implode. If you have PHP 7:
$list = implode(', ', array_column($output, 'id'));
For older versions:
$list = implode(', ', array_map(function($v) { return $v->id; }, $output));
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;
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;