PHP Unserialise Within an Array - php

I have an array that has the following values
[0] => array(4) {
["sku"] => string(12) "WMS-M-VN-MRN"
["name"] => string(62) "Maroon V-neck Jumper"
["qty_ordered"] => string(6) "1.0000"
["product_options"] => string(533) "a:2:{s:15:"info_buyRequest";a:6:{s:4:"uenc";s:64:"aHR0cDovL2VtYmxlbWF0aWMuY28udWsvd21zLW0tdm4tbXJuLz9fX19TSUQ9VQ,,";s:7:"product";s:3:"780";s:8:"form_key";s:16:"gDXvCEtQOlRWihqc";s:15:"related_product";s:0:"";s:7:"options";a:1:{i:1970;s:5:"17201";}s:3:"qty";s:1:"1";}s:7:"options";a:1:{i:0;a:7:{s:5:"label";s:27:"PLEASE SELECT SIZE REQUIRED";s:5:"value";s:12:"36 inch (13)";s:11:"print_value";s:12:"36 inch (13)";s:9:"option_id";s:4:"1970";s:11:"option_type";s:9:"drop_down";s:12:"option_value";s:5:"17201";s:11:"custom_view";b:0;}}}"
}
Now is there a way to unserialise the "Product_options" field in-line, without having to break the array apart and rebuild it again?

This is the way. You just have to reassign the result of unserialize to the same array item.
<?php
$array = [
[
"sku" => "WMS-M-VN-MRN",
"name" => "Maroon V-neck Jumper",
"qty_ordered" => "1.0000",
"product_options" => 'a:2:{s:15:"info_buyRequest";a:6:{s:4:"uenc";s:64:"aHR0cDovL2VtYmxlbWF0aWMuY28udWsvd21zLW0tdm4tbXJuLz9fX19TSUQ9VQ,,";s:7:"product";s:3:"780";s:8:"form_key";s:16:"gDXvCEtQOlRWihqc";s:15:"related_product";s:0:"";s:7:"options";a:1:{i:1970;s:5:"17201";}s:3:"qty";s:1:"1";}s:7:"options";a:1:{i:0;a:7:{s:5:"label";s:27:"PLEASE SELECT SIZE REQUIRED";s:5:"value";s:12:"36 inch (13)";s:11:"print_value";s:12:"36 inch (13)";s:9:"option_id";s:4:"1970";s:11:"option_type";s:9:"drop_down";s:12:"option_value";s:5:"17201";s:11:"custom_view";b:0;}}}'
]
];
$array[0]["product_options"] = unserialize($array[0]["product_options"]);
echo "<pre>";
print_r($array);
echo "</pre>";
Output:
Array
(
[0] => Array
(
[sku] => WMS-M-VN-MRN
[name] => Maroon V-neck Jumper
[qty_ordered] => 1.0000
[product_options] => Array
(
[info_buyRequest] => Array
(
[uenc] => aHR0cDovL2VtYmxlbWF0aWMuY28udWsvd21zLW0tdm4tbXJuLz9fX19TSUQ9VQ,,
[product] => 780
[form_key] => gDXvCEtQOlRWihqc
[related_product] =>
[options] => Array
(
[1970] => 17201
)
[qty] => 1
)
[options] => Array
(
[0] => Array
(
[label] => PLEASE SELECT SIZE REQUIRED
[value] => 36 inch (13)
[print_value] => 36 inch (13)
[option_id] => 1970
[option_type] => drop_down
[option_value] => 17201
[custom_view] =>
)
)
)
)
)

Related

search and unset array in array by value in php

My array look like this :
[cart_seller] => Array
(
[3] => Array
(
[หมวด1เลือก1 หมวด2เลือก1 1705] => Array
(
)
)
[4] => Array
(
[# 801] => Array
(
)
)
)
[cart_product] => Array
(
[หมวด1เลือก1 หมวด2เลือก1 1705] => Array
(
[id] => 1705
[name] => ทดสอบสินค้า
[image] => p1534937865-VASAVAT LAB N MEDIA LOGO W.png
[price] => 1111
[option] => หมวด1เลือก1 หมวด2เลือก1
[amount] => 1
)
[# 801] => Array
(
[id] => 801
[name] => โบว์แพรแถบ ร.9 ชนมพรรษา 84 พรรษา ปีพุทธศักราช 2554
[image] => p1498062217-ส.jpg
[price] => 90
[option] =>
[amount] => 1
)
)
I want unset '# 801' in cart_seller and cart_product
in cart_product use unset($cart['cart_product'][# 801]);
but in cart_seller it in array [4] what can i do without reference value (4) ?
exapmle unset($cart['cart_seller'][xxxx][# 801]);
Just loop cart seller array til you find #801.
foreach($cart['cart_seller'] as $key => $c){
if(array_key_exists("#801",$c)){
unset($cart['cart_seller'][$key]['#801']);
}
}

How to get the 0th index key values in array object

I have one array object in that object galleryImages i having like json string , in this galleryImages i want display onely the first image value , how can get the only first image value?
My array check below
$data=Array
(
[dbResultgallery] => Array
(
[0] => stdClass Object
(
[gallery_id] => 1
[className] => 1
[sectionName] => 1
[title] => Title 1
[description] => gfhfg
[galleryImages] => ["1.jpeg","2.jpg"]
[reg_on] => 2017-04-03 12:21:59
[created_by] => soupranjali#gmail.com
[school_id] => 2
[status] => 0
)
[1] => stdClass Object
(
[gallery_id] => 2
[className] => 1
[sectionName] => 1
[title] => Title 2
[description] => sdfdsfsdfsdfsdf
[galleryImages] => ["3.jpeg","4.jpg","5.jpg"]
[reg_on] => 2017-04-03 12:23:37
[created_by] => soupranjali#gmail.com
[school_id] => 2
[status] => 0
)
)
)
now i am using json_decode ,see below my code
foreach ($dbResultgallery as $gal) :
$role = json_decode($gal->galleryImages,true);
endforeach;
Now i am getting array format like
Array
(
[0] => 1.jpeg
[1] => 2.jpg
)
Array
(
[0] => 3.jpeg
[1] => 4.jpg
[2] => 5.jpg
)
Expected results
I need only the first images value like 1.jpeg and 3.jpeg, each array i need only the 0 th key values
Updated code
var_dump(json_decode($gal->galleryImages,true))
array(2) {
[0]=>
string(37) "6be1954c4bec91fe26fb7447fc551782.jpeg"
[1]=>
string(36) "153651d989591e76444c92cf037d5ac4.jpg"
}
array(3) {
[0]=>
string(37) "f356ceafa408e61cd1c62cfc39752b32.jpeg"
[1]=>
string(36) "a4fd29005696d92e0fc4cd3931454609.jpg"
[2]=>
string(36) "8eaaecdac1ff219192806acba7978a1b.jpg"
}
Change to this:
$role = json_decode($gal->galleryImages,true)[0];
foreach ($dbResultgallery as $gal) :
if(strpos(json_decode($gal->galleryImages,true) ,"jpeg")!== false){
echo 'found'; // do your stuff here
$role = json_decode($gal->galleryImages[0],true);
}
endforeach

php help: I have an array as a property of an object, and i cant seem to call the items in the array

I have a object 'aff' with an array 'bill' as a property.
var_dump($aff);
echo "<br/>";
print_r($aff->bill);
echo "<br/>";
var_dump($aff->bill);
echo "<br/>";
echo "<br/> $aff->bill[0]";
This returns:
affiliation Object ( [id] => 1 [afname] => TaylorGang [user1] => 18 [user2] => 19 [user3] => 0 [user4] => 0 [value1] => 65.432 [value2] => 52.14 [value3] => 0 [value4] => 0 [bill1] => Grape Soda [bill2] => Cable [bill3] => empty [bill4] => empty [due_date1] => 18 [due_date2] => 15 [due_date3] => 0 [due_date4] => 0 [startdate] => 2014-07-21 22:03:21 [user] => Array ( [0] => 18 [1] => 19 [2] => 0 [3] => 0 ) [bill] => Array ( [0] => Grape Soda [1] => Cable [2] => empty [3] => empty ) [value] => Array ( [0] => 65.432 [1] => 52.14 [2] => 0 [3] => 0 ) )
Array ( [0] => Grape Soda [1] => Cable [2] => empty [3] => empty )
array(4) { [0]=> string(10) "Grape Soda" [1]=> string(5) "Cable" [2]=> string(5) "empty" [3]=> string(5) "empty" }
Array[0]
I would expect that:
echo $aff->bill[0];
would return: 'Grape Soda'
If anyone could explain why this does not work and more importantly how I can get it to work it would be greatly appreciated.
Your string contains a complex expression, so you should wrap it up in curly braces:
echo "<br/> {$aff->bill[0]}";
Or, don't put the expression in the string (use concatenation):
echo "<br/> " . $aff->bill[0];
Without the curly braces, the [0] part is ignored as part of the expression, effectively leaving PHP to output the type of the object ("Array"), after which it treats the [0] as just part of the text, so the final output is effectively "<br/> " . "Array" . "[0]".
Read More

How To push array elements from one array to another array

Hello people here is the code that i was using initially....
array_push($Parameter_IdArray, $Parameter_Id1, $Parameter_Id2, $Parameter_Id3, $OptParameter_Id);
array_push($Eqt_ParamArray, $eqt_param1, $eqt_param2, $eqt_param3, $Opt_eqt_param1);
i had no issues to push array values .... but now $eqt_param1, $eqt_param2, $eqt_param3 and $Opt_eqt_param1 are in one more array it is something like this
Array ( [Profile_Id] => 4 [eqt_param] => Array ( [0] => 4.00 [1] => 4.00 [2] => 4.00 ) [Parameter_Id1] => 8 [min_param] => Array ( [0] => 1.00 [1] => 1.00 [2] => 1.00 ) [max_param] => Array ( [0] => 5.00 [1] => 5.00 [2] => 5.00 ) [Wtg_param] => Array ( [0] => 25.00 [1] => 25.00 [2] => 50.00 ) [Parameter_Id2] => 5 [Parameter_Id3] => 1 [Opt_eqt_param] => Array ( [0] => 0.00 ) [OptParameter_Id] => 14 [Opt_wtg] => Array ( [0] => 1.05 ) [operator] => Array ( [0] => M ) [eqt_pay] => 1,574,235 [rec_sal] => 1,485,000 [#] => -6.01 % [Emp_Id] => 490699 [Emp_Process] => Confirm New Pay )
now i need tp push array values $eqt_param1, $eqt_param2, $eqt_param3 and $Opt_eqt_param1 to $Eqt_ParamArray how to do that?
If I understand correctly, you've now got an associative array in PHP. On that assumption, I went ahead and reformatted the nightmare for you:
$myarray = array(
"Profile_Id" => 4,
"eqt_param" => array(4.00, 4.00, 4.00),
"Parameter_Id1" => 8,
"min_param" => array (1.00, 1.00, 1.00 ),
"max_param" => array (5.00, 5.00, 5.00 ),
"Wtg_param" => array (25.00, 25.00, 50.00 ),
"Parameter_Id2" => 5,
"Parameter_Id3" => 1,
"Opt_eqt_param" => array (0.00),
"OptParameter_Id" => 14,
"Opt_wtg" => array (1.05),
"operator" => array ("M"),
"eqt_pay" => array(1,574,235),
"rec_sal" => array(1,485,000),
"#" => "-6.01 %",
"Emp_Id" => 490699,
"Emp_Process" => "Confirm New Pay"
);
What this good formatting now makes clear is that you can use array_push directly on the "eqt_param" index:
array_push($myArray["eqt_param"], $eqt_param1, $eqt_param2, $eqt_param3, $Opt_eqt_param1);
You may also mean that you want to replace it, which is easy too:
$myArray['eqt_param'] = array($eqt_param1, $eqt_param2, $eqt_param3, $Opt_eqt_param1);
The same principles apply in Javascript, which you have had tagged so maybe you mean that.

how can i access such a php array

i was trying to access this php array with no luck, i want to access the [icon] => icon.png
Array ( [total] => 2
[total_grouped] => 2
[notifys] => Array ( [0] => Array ( [notifytype_id] => 12
[grouped] => 930
[icon] => icon.png
[n_url] => wall_action.php?id=930
[desc] => 690706096
[text] => Array ( [0] => Sarah O'conner ) [total] => 1 )))
$arr['notifys'][0]['icon']
ETA: I'm not sure what your comment means, the following code:
$arr = array('total'=>2, 'total_grouped' => 2, 'notifys' => array(array(
'notifytype_id' => 12, 'icon' => 'icon.png')));
echo '<pre>';
print_r($arr);
echo '</pre>';
var_dump($arr['notifys'][0]['icon']);
outputs:
Array
(
[total] => 2
[total_grouped] => 2
[notifys] => Array
(
[0] => Array
(
[notifytype_id] => 12
[icon] => icon.png
)
)
)
string(8) "icon.png"
Generally, code never outputs nothing. You should be developing with all errors and notifications on.
$arr['notifys'][0]['icon']
rg = Array ( [total] => 2 [total_grouped] => 2 [notifys] => Array ( [0] => Array ( [notifytype_id] => 12 [grouped] => 930 [icon] => icon.png [n_url] => wall_action.php?id=930 [desc] => 690706096 [text] => Array ( [0] => Sarah O'conner ) [total] => 1 )));
icon = rg["notifsys"][0]["icon"];
Everybody is posting right answer. Its just you have giving a wrong deceleration of array.
Try var_dump/print_r of array and then you can easily understand nodes.
$arr = array(total => 2,
total_grouped => 2,
notifys => array( 0 => array(notifytype_id => 12,
grouped => 930,
icon => 'icon.png',
n_url => 'wall_action.php?id=930',
desc => 690706096,
text =>array(0 => 'Sarah Oconner' ),
total => 1,
),
),
);
echo $arr['notifys']['0']['icon'];

Categories