I need to get product id with the unserialize php funcion. I have this text
a:1:{i:4;a:17:{s:8:"quantity";i:1;s:10:"product_id";i:5196;s:11:"category_id";s:3:"209";s:5:"price";d:1;s:3:"tax";s:5:"18.00";s:6:"tax_id";s:1:"1";s:11:"description";s:0:"";s:12:"product_name";s:4:"test";s:11:"thumb_image";s:0:"";s:3:"ean";s:0:"";s:10:"attributes";s:6:"a:0:{}";s:16:"attributes_value";a:0:{}s:6:"weight";s:6:"0.0000";s:9:"vendor_id";s:1:"0";s:5:"files";s:6:"a:0:{}";s:14:"freeattributes";s:6:"a:0:{}";s:25:"dependent_attr_serrialize";s:6:"a:0:{}";}}
and I get the product_id with this PHP code:
$rslt = unserialize($data);
echo $rslt[4]["product_id"]);
So my question is there a way to do something like echo $rslt[x]["product_id"];where x is any number between 0-9
Also tried this but doesn't work
$i=0;
while($rslt[$i]["product_id"]!="")
{
echo $i;
//echo $rslt[4]["product_id"];
echo $rslt[$i]["product_id"];
$i++;
}
Once you unserialize your input you have a good old PHP array, equivalent to:
$rslt = array (
4 =>
array (
'quantity' => 1,
'product_id' => 5196,
'category_id' => '209',
'price' => 1,
'tax' => '18.00',
'tax_id' => '1',
'description' => '',
'product_name' => 'test',
'thumb_image' => '',
'ean' => '',
'attributes' => 'a:0:{}',
'attributes_value' =>
array (
),
'weight' => '0.0000',
'vendor_id' => '0',
'files' => 'a:0:{}',
'freeattributes' => 'a:0:{}',
'dependent_attr_serrialize' => 'a:0:{}',
),
);
To grab the first element, just call current() as with any other array:
$first_item = current($rslt);
print_r($first_item['product_id']);
Related
i have a problem with this array, i need get sum form string merge with same key
$data = array(
0 => array(
'name' => 'Alfa Edison, Dwiki',
'budget' => 3700,
),
1 => array(
'name' => 'Maverick Sam',
'budget' => 500,
),
2 => array(
'name' => 'Dwiki',
'budget' => 1000,
),
3 => array(
'name' => 'Steve, Dwiki',
'budget' => 2000,
),
4 => array(
'name' => 'Alfa Edison',
'budget' => 700,
),
5 => array(
'name' => 'Maverick Sam',
'budget' => 4000,
),
6 => array(
'name' => 'Steve, Alfa Edison',
'budget' => 334,
),
);
i want the result this:
array(
0 => array(
'name' => 'Alfa Edison',
'budget' => 4734,
),
1 => array(
'name' => 'Dwiki',
'budget' => 6700,
),
2 => array(
'name' => 'Maverick Sam',
'budget' => 4500,
),
3 => array(
'name' => 'Steve',
'budget' => 2334,
),
);
How to merge String with same Key and Sum the Budget. i try to for each but i'm fail.
i try array_reduce and explode the name but fail.
The problem is that each of the "keys" (names) is really more than one key. So as you iterate the input array you'll need to split those up, then add an inner loop to use the names as keys in the result.
foreach ($data as $item) {
// separate the names
$names = explode(', ', $item['name']);
// iterate the names and set/increase values for them in the result array
foreach ($names as $name) {
$result[$name]['name'] = $name;
$result[$name]['budget'] = $item['budget'] + ($result[$name]['budget'] ?? 0);
}
}
// remove the string keys if necessary
$result = array_values($result);
I am getting some data out of mysql into an array like this
array(
[0] = array(
'code' => '123456',
'title' => 'something',
'price' => '2.00',
'other_value' => '555555'
),
[1] = array(
'code' => '123456',
'title' => 'something',
'price' => '2.00',
'other_value' => '666666'
),
[2] = array(
'code' => '234567',
'title' => 'something else',
'price' => '3.00',
'other_value' => '333333'
),
[3] = array(
'code' => '345678',
'title' => 'another thing',
'price' => '4.00',
'other_value' => NULL
),
)
what i need to do is for each row, if the key code appears more than once, merge the rows into one but create a new array for the other_value like so
array(
[0] = array(
'code' => '123456',
'title' => 'something',
'price' => '2.00',
'other_value' => array(
[0] => '555555',
[1] => '666666'
)
),
[1] = array(
'code' => '234567',
'title' => 'something else',
'price' => '3.00',
'other_value' => '333333'
),
[2] = array(
'code' => '345678',
'title' => 'another thing',
'price' => '4.00',
'other_value' => NULL
),
)
What is the best way to achieve this?
I did think about looping over the each row and checking for existence of thtat key/value then do something if it exists.
#AdRock i hope you want to merge array in case of when 'code' will be same, if this is so then try below one:
<?php
$arr = array(
array(
'code' => '123456',
'title' => 'something',
'price' => '2.00',
'other_value' => '555555'
),
array(
'code' => '123456',
'title' => 'something',
'price' => '2.00',
'other_value' => '666666'
),
array(
'code' => '234567',
'title' => 'something else',
'price' => '3.00',
'other_value' => '333333'
),
array(
'code' => '345678',
'title' => 'another thing',
'price' => '4.00',
'other_value' => NULL
)
);
echo "<pre>";
print_r($arr);// array before
$isExist = array();
foreach($arr as $key => $value){
if(in_array($value["code"], $isExist)){
$getKey = array_search($value["code"], $isExist);
$arr[$getKey]["other_value"] = array($arr[$getKey]["other_value"], $value["other_value"]);
unset($arr[$key]);
}
else{
$arr[$key] = $value;
}
$isExist[$key] = $value["code"];
}
echo "<pre>";
print_r(array_values($arr));// array after
?>
The approach that I would suggest is to loop through the array and store the value of code into a new array and store the entire result set into a new array. With each iteration, check whether the value is present or not in the code value stored array. And if value found then in that case, get the key of the code array and use the same key for the result array and store it inside the other_value. Hope it makes sense.
Looping over the array and creating a new array using the 'code' values as keys might be the simplest method. In that case you can check if the key allready exists.
$new_array=array();
foreach($array as $part){
$code_as_key = $part['code'];
//if code allready in the new array, just add a new value
if( isset($new_array[$code_as_key]) ){
$new_array[$code_as_key]['other_value'][] = $part['other_value'];
}
//else add the new code
else{
$new_array[$code_as_key]=$part;
}
}
//re-index the new array, starting from key 0
$new_array=array_values($new_array);
I searched for few in-built methods and tried few logics from here and there but none can make me satisfied. I have created my own logic but is limited to a certain dimension of the array. I have managed to solve my problem for the project but as I said I am not satisfied.
Consider the array below:
$data = array
(
'0' => array
(
'0' => array
(
'make_ready' => '',
'ready_time' => '0.55',
'rate' => '46',
'no_of_run' => '',
'fixed_cost' => '',
'variable_cost' => '25.3',
'0' => array(
'kg' => 2.66,
'rate' => 11.4,
'fixed_cost' => '',
'variable_cost' => 30.32,
'0' => array(
'kg' => 2.66,
'rate' => 11.4,
'fixed_cost' => '',
'variable_cost' => 30.32,
),
),
),
),
'1' => array
(
'0' => array
(
'make_ready' => '1',
'ready_time' => '1.16',
'rate' => '36.47',
'no_of_run' => '',
'fixed_cost' => '36.47',
'variable_cost' => '42.31',
),
),
'2' => array
(
'make_ready' => '2',
'ready_time' => '0.29',
'rate' => '360',
'no_of_run' => '',
'fixed_cost' => '720',
'variable_cost' => '104.4',
),
'size' => '1000 X 1200 X 1190',
'up' => '3 X 4 / 17',
'unit' => '4',
'rate' => 16.32,
'fixed_cost' => '',
'variable_cost' => 65.28,
);
Problem:
I want to sum up all the values of array element where key is 'variable_cost', no matter how deep (in terms of dimension) is the array. Basically like a loop that can scan all the elements and do 'sum' if key matches.
I would like someone to help/suggest any logic where the calculations can be done till nth dimension.
For example like the movie Inception, in which they can go
dream->with in a dream->to nth number of dream.
and come out with the sum of 'variable_cost'. Hope you guys can understand the question here.
Thank you.
You will need to use a recursive function to sum all the variable_cost values:
function sum_variable_cost( $data, $total=0) {
foreach ($data as $key => $item) {
if (is_array($item)) {
$total = sum_variable_cost($item, $total);
} else if ($key=='variable_cost') {
//echo $key . " " . $item . "\n";
$total += $item;
}
}
return $total;
}
echo sum_variable_cost( $data );
Basically it loops through the array and sees if there are other arrays and only adds values that contain the key variable_cost.
If you uncomment the echo line it will show this output:
variable_cost 25.3
variable_cost 30.32
variable_cost 30.32
variable_cost 42.31
variable_cost 104.4
variable_cost 65.28
297.93
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'];
}
Ok I have a mult-dimensional array which has the following structure...
0 =>
array (
'membership' =>
array (
'member' =>
array (
'name' => '',
'landline' => '',
'libcard' => '',
'mobile' => '',
'email' => '',
),
'updated_at' => '',
'member_id' => 12345,
'starts_at' => '',
'id' => 14,
'group_id' => 280,
'optional_field_values' =>
array (
0 =>
array (
'optional_field' =>
array (
'name' => '',
'updated_at' => '',
'id' => 1,
'group_id' => 280,
'description' => '',
'created_at' => '',
),
'updated_at' => '',
'optional_field_id' => 1,
'membership_id' => 14,
'id' => 4,
'value' => '12539267',
'created_at' => '',
),
),
'ends_at' => '',
'joining_fee' => 0,
'created_at' => '',
),
),
Now I can access everything inside Membership and inside Member using code like...
$member[0]['membership']['member']['name']
or
$member[0]['membership']['joining_fee']
But when ever I try to access stuff inside optional_field_values I get nothing returned...
Any ideas why this is not working?
Edit:
Trying to access the field using code like...
$member[0]['membership']['optional_field_values']['value']
$member[0]['membership']['optional_field_values'][0]['value']
^ Should work...
(Edited to match OP's edit)
How about :
$member[0]['membership']['optional_field_values'][0]['value']
You can iterate over all optional field values like this :
foreach ($member[0]['membership']['optional_field_values'] as $field)
echo $field['value'];