I'm writing a custom module for one of my projects and I'm having a bit of an issue. What I'm trying to do is get a piece of data from uc_cart_get_contents() into a variable so that I can use it in a calculation:
Here's my var_export:
array ( )array ( 0 => stdClass::__set_state(array( 'cart_item_id' => '5', 'cart_id' => '1', 'nid' => '9', 'qty' => '10', 'changed' => '1287074120', 'data' => array ( 'attributes' => array ( 3 => '4', ), 'shippable' => '1', 'restrict_qty' => '0', 'module' => 'uc_product', ), 'title' => 'Chicago Canyon', 'vid' => '9', 'cost' => 1.16, 'price' => 11.16, 'weight' => 0, 'module' => 'uc_product', 'model' => 'chicago-canyon-p-card-p-env', )), )
But what if I want to put the model of each item in the cart into a variable, and based upon that variable adjust the item's price.
If it was an array I would do this:
$items= uc_cart_get_contents();
$model = $items[model];
but that doesn't work, it's a std class. I'm so so lost. Please help!
If your asking how to access object propertiers you do so like this:
$model = $items->model;
for starters, uc_cart_get_contents() returns an array of objects. so, if you did:
$items=uc_cart_get_contents();
you would have to iterate over $items, and then get the specific data elements you want from each item.
for example:
$items = uc_cart_get_contents();
foreach ($items as $item)
{
$model = $item->model;
// whatever ...
}
hope that helps
Related
I am working with an API and i am trying to sort the json object it returns to me. Here is a small cut out of the json object
Now i want to sort this because i only want to save part of the data. Say i wanted to save the "_ID" here. THe data i need is really way further down but i think if i can figure out how to get this, i can figure out the rest. Since as you can see from the picture, it contains many objects inside objects and arrays within arrays its very confusing for me to target the specific values i want
I have tried something like this:
$body = $response->getBody();
$data = json_decode($body, true);
$sortedData = $data."hits".hits[0]."_id";
return $sortedData
Also tried without the quotation marks, but i cant really get it to work. Im very new to PHP.
You decoded these objects into associative arrays, so $data['hits']['hits'][0]['_id'] should be the correct way to access that element.
https://www.php.net/manual/en/language.types.array.php explains the basics of how to work with arrays and access elements within them.
Since php 5.5, you can use array_column to get specific column from the array :
$body = $response->getBody();
$data = json_decode($body, true);
$sortedData = array_column($data, '_id');
return $sortedData;
Edit
From what I see on your array sample, you want to do a recursive multidimensional arrays search (not just 2-dimensional).
Assuming you have array like this :
$arr = array(
array(
'x' => '10',
'name' => 'blah',
'other' => array(
'_id' => '26',
'color' => '10x',
)
),
array(
'x' => '7',
'name' => 'blahblah',
'other' => array(
'_id' => '29',
'color' => '7x',
)
),
array(
'x' => '15',
'name' => 'blahblahblah',
'other' => array(
'_id' => '27',
'color' => '15x',
)
),
array(
'x' => '1',
'name' => 'sdf',
'other' => array(
'_id' => '41',
'color' => '1x',
)
),
array(
'x' => '4',
'name' => '3dgdg',
'other' => array(
'_id' => '31',
'color' => '4x',
)
),
array(
'x' => '5',
'name' => 'nmnmnm',
'other' => array(
'_id' => '36',
'color' => '5x',
)
),
array(
'x' => '21',
'name' => 'dhshshhh',
'other' => array(
'_id' => '34',
'color' => '21x',
)
)
);
And you want to get the _id key. In that case you could try this function :
function array_column_recursive(array $haystack, $needle) {
$found = [];
array_walk_recursive($haystack, function($value, $key) use (&$found, $needle) {
if ($key == $needle)
$found[] = $value;
});
return $found;
}
Usage :
$ids = array_column_recursive($arr,'_id');
sort($ids);
print_r($ids);
Reference :
array_column
array_column_recursive
I'm trying to insert a couple of new Key/Value pairs into an associative array at a specific place. From other reading I've done on SO, I'm pretty sure I have to loop through the array and insert the new values when a condition is set.
Here is the current array
array(
(int) 0 => array(
'Product' => array(
'id' => '59',
'title' => ' Blue Dress',
'Review' => array(
'id' => '7',
'product_id' => '59',
'Review' => array(
(int) 0 => array(
'average' => '3.0000'
)
)
)
)
)
(int) 1 => array(
'Product' => array(
'id' => '60',
'title' => 'Red Dress',
'Review' => array()
)
)
)
The key Review does not always have data, but when it does I want to insert a new key-value similar to the following excerpt
(int) 0 => array(
'Product' => array(
'id' => '59',
'title' => ' Blue Dress',
'Review' => array(
'id' => '7',
'product_id' => '59',
'Review' => array(
(int) 0 => array(
'average' => '3.0000'
'some_value' => '5'
)
)
)
)
)
I've tried a few things without success.
Any help is much appreciated thanks.
You can do something like this:
if(!empty($your_array[index]['Product']['Review'])){
$your_array[index]['Product']['Review']['Review'][index]['some_value'] = 'new_value';
}
In your example it could be:
if(!empty($your_array[0]['Product']['Review'])){
$your_array[0]['Product']['Review']['Review'][0]['some_value'] = 'new_value';
}
Again, you didn't mention your code. So, it's hard to figure out what you want exactly!
You should iterate through Your array and pass current value be reference:
// Notice & sign before variable
foreach ($data as &$product)
{
if ($product['Product']['Review'])
{
// or iterate through Review array
$product['Product']['Review']['Review'][0]['some_value'] = 5;
}
}
I have problem with my code here, I want convert serialize data in wordpress like this
$data ='a:2:{i:0;a:8:{s:8:"order_id";s:2:"19";s:5:"print";s:18:"type-canvas-framed";s:4:"size";s:12:"08-x-10-inch";s:18:"frame_canvas_color";s:10:"blackframe";s:11:"orientation";s:8:"portrait";s:3:"qty";s:1:"1";s:5:"price";d:42.990000000000002;s:8:"shipping";d:13.800000000000001;}i:1;a:7:{s:8:"order_id";s:2:"19";s:5:"print";s:11:"type-poster";s:4:"size";s:12:"36-x-48-inch";s:11:"orientation";s:8:"portrait";s:3:"qty";s:1:"1";s:5:"price";d:42.990000000000002;s:8:"shipping";d:14.800000000000001;}}' ;
I do parse the data using unseriliaze using unserialize the result like this
$result=array (
0 =>
array (
'order_id' => '19',
'print' => 'type-canvas-framed',
'size' => '08-x-10-inch',
'frame_canvas_color' => 'blackframe',
'orientation' => 'portrait',
'qty' => '1',
'price' => 42.99,
'shipping' => 13.8,
),
1 =>
array (
'order_id' => '19',
'print' => 'type-poster',
'size' => '36-x-48-inch',
'orientation' => 'portrait',
'qty' => '1',
'price' => 42.99,
'shipping' => 14.8,
),
);
I want to looping the array, how to do that in wordpress.
Thanks
you dont need wordpress specific functions for that use:
foreach($result as $key => $value){
// process array
}
just use foreach
foreach($result as $key => $value ) {
echo $value['order_id'];
}
I have a code:
'items' =>
array (
0 =>
array (
'name' => 'paste',
'qty' => 5,
'price' => 2.5,
),
1 =>
array (
'name' => 'soap',
'qty' => 5,
'price' => 2.5,
),
)
I am trying to write a query to find out the index of the value where the sub-document items has a data entry of name as "soap". Can anyone help me with the working solution?
I think you're after something along the lines of
foreach($items as $key => $value){
if($value['name'] == "soap") $output = $key;
}
Where $output would be the key of the array with item set to soap
I've an associated array with 20 elements whose child array looks like
(int) 2 => array(
'Comment' => array(
'id' => '5',
'user_id' => '13',
'time' => '2012-05-18 14:47:36'
),
'User' => array(
'name' => 'User name'
)
)
Now I want to extract the field name from its child array User with cakephp's set utility and append it to the child array Comment. Is there a one way step to do this other than using a for or foreach loop?
(int) 2 => array(
'Comment' => array(
'id' => '5',
'user_id' => '13',
'time' => '2012-05-18 14:47:36',
'name =>'User name'
)
)
It doesn't answer your question, but doing this with a foreach is a one way step too:
foreach ($arrays as $array)
$array['Comment']['name'] = $array['User']['name'];
I believe there's no need to use some utility, just for the sake of using an utility.
Especially that it'll do a foreach itself.