Trying to remove session array from array PHP - php

Im trying to remove a session['cart_items] array from a multi array and I can't seem to get the delete part to work.
I'm getting the id through: $_POST['product] however I've tried the following and getting a bit stuck:
$temp = array_flip($_SESSION['cart_items']);
unset($_SESSION['cart_items'][$temp[$_POST['product']]]);
I've also tried:
unset($_SESSION['cart_items'][$key]);
My output is:
Array
(
[0] => Array
(
[0] => Array
(
[item_id] => 407
[item_name] => Am I Bothered? About Homophobia
[item_qty] => 22
)
)
)
An help would be great
Multiple array result:
array(1) {
["cart_items"]=>
array(2) {
[1]=>
array(1) {
[0]=>
array(3) {
["item_id"]=>
string(3) "407"
["item_name"]=>
string(31) "Am I Bothered? About Homophobia"
["item_qty"]=>
string(2) "50"
}
}
[2]=>
array(1) {
[0]=>
array(3) {
["item_id"]=>
string(4) "1131"
["item_name"]=>
string(50) "10 Ways A Condom Can’t Protect You – Postcards"
["item_qty"]=>
string(2) "14"
}
}
}
}

for(i=0;i<sizeof($_SESSION['cart_items'][0]);i++){
if($_SESSION['cart_items'][0][i]['item_id'] == $key);
unset($_SESSION['cart_items'][0][i]['item_id']);
}

Related

Illigal String Offset 'id' PHP

RESOLVED: I WAS ADDING EXTRA INDEX TO $firms LIKE $firms['type'] = 'ABC' AS $firms Already Contains CHILD ARRAYS. My BAD, Sorry.
This might be duplicate But i couldn't figure it out T _ T Sorry
As i am printing array results coming right from database, its continuously annoying me with error
Illegal string offset 'id'
When i simply print_r result array it gives me following result.
echo "<pre>";
foreach ($firms as $firm) {
print_r($firm[0]);
}
echo "</pre>";
Result:
Array
(
[id] => 7923
[rank] => 0
[name] => CBRE
)
Array
(
[id] => 7919
[rank] => 0
[name] => Aecom
)
But if i try to print 'id' it gives me error:
echo "<pre>";
foreach ($firms as $firm) {
echo $firm[0]['id'];
}
echo "</pre>";
Error:
Illegal string offset 'id'
VAR_DUMP($firms) , Result:
array(3) {
[0]=>
array(1) {
[0]=>
array(3) {
["id"]=>
int(7923)
["rank"]=>
int(0)
["name"]=>
string(4) "CBRE"
}
}
[1]=>
array(1) {
[0]=>
array(3) {
["id"]=>
int(7919)
["rank"]=>
int(0)
["name"]=>
string(5) "Aecom"
}
}
[2]=>
array(1) {
[0]=>
array(3) {
["id"]=>
int(6793)
["rank"]=>
int(1)
["name"]=>
string(5) "AECOM"
}
}
}

How to sort 3D arrays in php?

How do I sort 3D arrays ? In this case I want to sort after date. Thx ?
array(2) {
["garsoniere"]=>array(2) {
[0]=> array(5) {
["date"]=> string(19) "2014-02-04"
["sponsored"]=> string(1) "0"
["offer_status"]=> string(6) "active"
["suprafata"]=> string(3) "111"
["confort"]=> string(2) "-1"
["title"]=> string(18) "Jimmy Humununukaua"
}
[1]=> array(5) {
["date"]=> string(19) "2013-03-25"
["sponsored"]=> string(1) "0"
["offer_status"]=> NUL
["suprafata"]=> string(2) "23"
["confort"]=> NULL
["title"]=> string(38) "Garsoniera de vanzare in Marasti, Cluj"
}
}
["apartamente"]=> array(2) {
[0]=> array(5) {
["date"]=> string(19) "2014-02-05"
["sponsored"]=> string(1) "0"
["offer_status"]=> string(6) "active"
["etaj"]=> string(2) "50"
["title"]=> string(15) "Test Apartament"
}
[1]=> array(5) {
["date"]=> string(19) "2014-02-04"
["sponsored"]=> string(1) "0"
["offer_status"]=> string(6) "active"
["etaj"]=> string(2) "50"
["title"]=> string(13) "dfasfsdffasdf"
}
}
}
I know it looks wrong but I want that the result should look like this:
["apartamente"][0][date]=>2014-02-05.....[title]
["apartamente"][1][date]=>2014-02-04.....[title]
["garsoniere"][0][date]=>2014-02-04.....[title]
["garsoniere"][1][date]=>2013-03-25.....[title]
Can anyone help me with this ?
Please use usort function to achieve this. Please have a look at http://badarwaqas.blogspot.com/2013/04/php-sorting-array-on-nth-level-key.html article
Use usort
Inital array
Array
(
[giraffe] => Array
(
[date] => 2014-02-04
)
[zebra] => Array
(
[date] => 2012-02-04
)
[penguin] => Array
(
[date] => 2014-01-04
)
)
The sorting algorithm
usort($arr, function($a, $b) {
return ($a['date'] > $b['date'] ? 1 : ($a['date'] < $b['date'] ? -1 : 0));
});
Which gives - after sorting
Array
(
[0] => Array
(
[date] => 2012-02-04
)
[1] => Array
(
[date] => 2014-01-04
)
[2] => Array
(
[date] => 2014-02-04
)
)
Code grabbed from here
Try
$result = array();
foreach($arr as $key=>$val){
foreach($val as $key1=>$val1){
$result[$key][$key1]= $val1;
}
}
ksort($result,SORT_NATURAL);
I am not sure about the sort you have specified in your question.
See demo here

Access to PHP array elements

I have a PHP array when I used var_dump() this is the result I get:
array(1) { ["GetVehicleConfigurationByVehicleIdResult"]=> array(9) { ["Id"]=> string(1) "2" ["VIN"]=> NULL ["Year"]=> array(2) { ["Id"]=> string(4) "2006" ["Value"]=> string(4) "2006" } ["Make"]=> array(2) { ["Id"]=> string(1) "2" ["Value"]=> string(5) "Acura" } ["Model"]=> array(2) { ["Id"]=> string(1) "2" ["Value"]=> string(2) "TL" } ["Trim"]=> array(2) { ["Id"]=> string(6) "268650" ["Value"]=> string(12) "3.2 Sedan 4D" } ["Mileage"]=> string(6) "100000" ["OptionalEquipment"]=> array(1) { ["EquipmentOption"]=> array(35) { [0]=> array(13) { ["DisplayName"]=> string(19) "V6, VTEC, 3.2 Liter" ["VehicleOptionId"]=> string(3) "204" ["IsSelected"]=> string(4) "true" ["OptionTypeDisplayName"]=> string(6) "Engine" ["OptionGroupName"]=> string(3) "N/A" ["DisplayNameAdditionalData"]=> string(3) "N/A" ["ManufactureCode"]=> string(0) "" ["OptionAvailabilityDisplayName"]=> string(3) "N/A" ["IsDefaultConfiguration"]=> string(4) "true" ["DetailName"]=> string(3) "N/A" ["NonBoldName"]=> string(3) "N/A" ["Footer"]=> string(3) "N/A" ["SortOrder"]=> string(4) "1000" }
How I can get the elements from this array?
Some of the elements are complex, like they are array inside array.
That is the formatted print out of the array to understand better:
Array
(
[GetVehicleConfigurationByVehicleIdResult] => Array
(
[Id] => 2
[VIN] =>
[Year] => Array
(
[Id] => 2006
[Value] => 2006
)
[Make] => Array
(
[Id] => 2
[Value] => Acura
)
[Model] => Array
(
[Id] => 2
[Value] => TL
)
[Trim] => Array
(
[Id] => 268650
[Value] => 3.2 Sedan 4D
)
[Mileage] => 100000
[OptionalEquipment] => Array
(
[EquipmentOption] => Array
(
[0] => Array
(
[DisplayName] => V6, VTEC, 3.2 Liter
[VehicleOptionId] => 204
[IsSelected] => true
[OptionTypeDisplayName] => Engine
[OptionGroupName] => N/A
[DisplayNameAdditionalData] => N/A
[ManufactureCode] =>
[OptionAvailabilityDisplayName] => N/A
[IsDefaultConfiguration] => true
[DetailName] => N/A
[NonBoldName] => N/A
[Footer] => N/A
[SortOrder] => 1000
)
I want to get: Id, VIN, Year, Make, Model, Trim,Mileage and OptionalEquipment and pass them as 1 single parameter to another method.
It solved:
$Id = $resultVehicleId['GetVehicleConfigurationByVehicleIdResult']['Id'];
$Year = $resultVehicleId['GetVehicleConfigurationByVehicleIdResult']['Year']['Value'];
You are correct. There are arrays inside of arrays here. And var_dump shows it very nicely so you can perfectly know how to navigate the levels of this multi-dimensional array.
If you want VIN just get $array['GetVehicleConfigurationByVehicleIdResult']['VIN']
For Year you need to get $array['GetVehicleConfigurationByVehicleIdResult']['Year']['Value']
I think you can guess the others now.
They are just array elements no matter how deep you go so you can just reference them by name like below:
$id = $that_array['GetVehicleConfigurationByVehicleIdResult']['Id'];
$id = $array_name["GetVehicleConfigurationByVehicleIdResult"]['Id'];;
$vin = $array_name["GetVehicleConfigurationByVehicleIdResult"]['VIN'];
$year = $array_name["GetVehicleConfigurationByVehicleIdResult"]['Year']
$make = $array_name["GetVehicleConfigurationByVehicleIdResult"]['Make'];
$model = $array_name["GetVehicleConfigurationByVehicleIdResult"]['Model'];
$trim = $array_name["GetVehicleConfigurationByVehicleIdResult"]['Trim']['Value'];
$mileage = $array_name["GetVehicleConfigurationByVehicleIdResult"]['Mileage'];
$optional_equipment = $array_name["GetVehicleConfigurationByVehicleIdResult"]['OptionalEquipment']['EquipmentOption'][0]['DisplayName'];
For simplicity's sake I'm assuming this array is saved as $array.
You can access the data from the array like this:
$vin = $array['GetVehicleConfigurationByVehicleIdResult']['VIN'];
But you stated that you want to pass them all as a single parameter, so to do that you would probably want to just pass an array.
someFuntion($array['GetVehicleConfigurationByVehicleIdResult']);

php multidimensional array: how to remove duplicate entries

in fear of duplicating content, i have looked through so many similar SO questions, but i think i need a bit more than code, to tell me how solve my problem- would be lovely with some explaination too.
How do i turn $list:
array(4) {
[0]=>
array(2) {
["title"]=>
string(8) "Zambezia"
["id"]=>
int(31)
}
[1]=>
array(2) {
["title"]=>
string(6) "Zarafa"
["id"]=>
int(34)
}
[2]=>
array(2) {
["title"]=>
string(8) "Zambezia"
["id"]=>
int(31)
}
[3]=>
array(2) {
["title"]=>
string(8) "Zambezia"
["id"]=>
int(31)
}
}
Into $list:
array(2) {
[0]=>
array(2) {
["title"]=>
string(8) "Zambezia"
["id"]=>
int(31)
}
[1]=>
array(2) {
["title"]=>
string(6) "Zarafa"
["id"]=>
int(34)
}
}
By removing duplicate entries?
Use array_unique() with SORT_REGULAR flag.
$new_array = array_unique($array, SORT_REGULAR);
Output should be:
Array
(
[0] => Array
(
[title] => Zambezia
[id] => 31
)
[1] => Array
(
[title] => Zarafa
[id] => 34
)
)
Demo.

Having trouble with a recursion algo

Okay here is my recursion algo:
public function getCategoryTree($tree,$return = array()) {
foreach ($tree->children as $child) {
if (count($child->children) > 0 )
$return[$tree->name] = $this->getCategoryTree($child, $return);
else
$return[] = $child->name;
}
return $return;
}
Here is a snippet of the data structure I'm trying to traverse
Object(stdClass)#290 (6) {
["category_id"]=>
int(1)
["parent_id"]=>
int(0)
["name"]=>
string(4) "Root"
["position"]=>
int(0)
["level"]=>
int(0)
["children"]=>
array(2) {
[0]=>
object(stdClass)#571 (7) {
["category_id"]=>
int(2)
["parent_id"]=>
int(1)
["name"]=>
string(18) "Root MySite.com"
["is_active"]=>
int(0)
["position"]=>
int(0)
["level"]=>
int(1)
["children"]=>
array(11) {
[0]=>
object(stdClass)#570 (7) {
["category_id"]=>
int(15)
["parent_id"]=>
int(2)
["name"]=>
string(9) "Widgets"
["is_active"]=>
int(1)
["position"]=>
int(68)
["level"]=>
int(2)
["children"]=>
array(19) {
[0]=>
object(stdClass)#566 (7) {
["category_id"]=>
int(24)
["parent_id"]=>
int(15)
["name"]=>
string(16) "Blue widgets"
["is_active"]=>
int(1)
["position"]=>
int(68)
["level"]=>
int(3)
["children"]=>
array(0) {
}
}
<snip....>
I'm trying to get a php data structure like such
categories = array( "Root" =>
array("Root MySite.com" =>
array( "Widgets" =>
// final element is NOT an array
array ("Blue Widgets", "Purple Widgets" ...)
)
)
)
I can't quite seem to get the data structure i'm looking for using my recursive algo. Any help
would be great.
Eventually I'll need to parse it again on the frontend and display it, but another problem for another day...
Have a look at this phpFiddle for a full working example. The only error I found was the $this->getCategoryTree which gave me an Fatal Error Using $this when not in object context. So are you sure the function is within the correct scope?
Updated
I hope this one works. :)
function traverse($root, $return = array()) {
$return[$root->name] = array();
foreach ($root->children as $child) {
if (count($child->children) > 0) {
traverse($child, &$return[$root->name]);
}else {
array_push(&$return[$root->name], $child->name);
}
}
return $return;
}
The output from this is:
Array ( [Root] =>
Array (
[Root MySite.com] =>
Array (
[Widgets] => Array ( [0] => Blue Widget [1] => Purple Widget)
[Gizmos] => Array ( [0] => Blue Gizmos [1] => Purple Gizmos)
)
[FooBar.com] =>
Array (
[Widgets] => Array ( [0] => Blue Widget [1] => Purple Widget)
[Gizmos] => Array ( [0] => Blue Gizmos [1] => Purple Gizmos)
)
)
)
Again, full working example

Categories