Add/Change Array to/in existing PHP $_SESSION - php

I got following array in my PHP $_SESSION
[CART] => SHOPPINGCART OBJECT
(
[CONTENTS] => ARRAY
(
[121] => ARRAY
(
[QTY] => 1
)
I know how to change some simple $_SESSION variable, but what if there is an array in array and then there is the value I want to change? Or what if i wanted to add a new Array to [CONTENTS]? Sadly i couldn't find a solution on here.

Your $_SESSION array actually contains an OBJECT which contains and ARRAY which contains an ARRAY
So using normal notation to access QTY for example you would do
echo $_SESSION['CART']->CONTENTS[121]['QTY'];
Or changing the QTY
$_SESSION['CART']->CONTENTS[121]['QTY'] = 10;
Adding a new array to CONTENTS would be
$_SESSION['CART']->CONTENTS[] = array('QTY' => 2);
Then viewing all the CONTENTS array you could do
foreach ( $_SESSION['CART']->CONTENTS as $id => $content ) {
echo "$id\n"
foreach ( $content as $qty ) {
echo " $qty\n";
}
}

One could use a combination of serialize()/unserialize().
To store it in a session:
$cart= new SHOPPINGCART();
$_SESSION['cart'] = serialize($cart);
To actually change values from it:
$cart = unserialize($_SESSION['cart']);
// do anything with cart and store it afterwards with the code above
$cart["quantity"] += 1; // increment it by one
$_SESSION['cart'] = serialize($cart);

$temp = $_SESSION['CART'];
$temp->CONTENTS['121']['QTY'] = 5;
$_SESSION['CART'] = $temp;
Check if this works; then we can think of a way for specifying "121".

Related

Appending new data to object

I'm stuck trying to append new data to an stdClass object that I'm creating for an AMchart
I'm returning all the rows I want from the DB, then creating a new object and looping through the returned array, but rather than appending what I want to the end of the existing object, it just gets overwritten. PHP objects dont have an append or push method, so how do you accomplish this?
Here's what my code looks like. Am I missing something simple?
$sql = 'SELECT
count(*) as clients,
STR_TO_DATE(Appt_date, \'%m/%d/%Y\') AS date,
SUM(wait_time) as total_wait_time
FROM tb_by_client
WHERE status = #qualifier
GROUP BY Appt_date';
$rows = $db->fetchAll($sql);
$chartObject = new stdClass();
foreach($rows as $row){
$row->average = round($row->total_wait_time / $row->clients);
$chartObject->date = $row->date;
$chartObject->average = $row->average;
}
$chartArray[] = $chartObject;
return json_encode($chartArray);
So instead of getting something that looks like this
[{"date":"2018-10-01","average":12},{"date":"2018-10-02","average":-33},{"date":"2018-10-04","average":23},{"date":"2018-10-05","average":6}]
I get back just a single
[{"date":"2018-10-01","average":12}]
Because each loop overwrites the last key and value
How do you append instead?
Your problem is you overwrite the data without saving it
$chartObject = new stdClass();
foreach($rows as $row){
$row->average = round($row->total_wait_time / $row->clients);
$chartObject->date = $row->date;
$chartObject->average = $row->average;
}
$chartArray[] = $chartObject;
See on each iteration of foreach($rows as $row){ you change the data in $chartObject, but you never save in your $chartArray.
Do this instead
foreach($rows as $row){
$chartObject = new stdClass(); //new instance of stdClass, obj pass by refrence
$row->average = round($row->total_wait_time / $row->clients);
$chartObject->date = $row->date;
$chartObject->average = $row->average;
$chartArray[] = $chartObject;
}
Personally I wouldn't even bother with using an object:
foreach($rows as $row){
$average = round($row->total_wait_time / $row->clients);
$chartArray[] = ['date'=>$row->date,'average'=>$average];
}
When you JSON Encode an array with string keys, it will make it the correct Javascript Object structure. So there really is no need to keep all those objects in memory and the code is much smaller, cleaner, and easier to read.
One last thing I hinted at in the code, is that objects are pass by reference in PHP (now), and if you don't create a new instance of the object for each iteration, you will actually update all references to the object. This can be illustrated like this:
$obj = new stdClass;
$objects = [];
for($i=0;$i<3;++$i){
$obj->foo = $i;
$objects[] = $obj;
print_r($objects);
}
Output:
Array
(
[0] => stdClass Object
(
[foo] => 0
)
)
Array
(
[0] => stdClass Object
(
[foo] => 1
)
[1] => stdClass Object
(
[foo] => 1
)
)
Array
(
[0] => stdClass Object
(
[foo] => 2
)
[1] => stdClass Object
(
[foo] => 2
)
[2] => stdClass Object
(
[foo] => 2
)
)
Sanbox
Each array is a single iteration of the for loop, this is the same array with another row added after each iteration.
As you can see each copy (its not really a copy) is updated by reference in the array. Basically we have stored the same object (instance ,will call him Bob) 3 times, instead of 3 separate objects (Bob, Alice, John).
If the data you stored was the color of a persons shirt, when Bob puts on a red shirt, he has a red shirt on, but Alice and John don't.
Because of this you need to create a new instance of the object for each iteration and store that.
Hope that helps!
You can do the maths in the SQL and it cuts out the loop altogether...
$sql = 'SELECT STR_TO_DATE(Appt_date, \'%m/%d/%Y\') AS date,
round(SUM(wait_time)/count(*)) as average
FROM tb_by_client
WHERE status = #qualifier
GROUP BY Appt_date';
return json_encode($db->fetchAll($sql));
You're misunderstanding what should be in the loop and what shouldn't.
This should be fine:
$sql = 'SELECT
count(*) as clients,
STR_TO_DATE(Appt_date, \'%m/%d/%Y\') AS date,
SUM(wait_time) as total_wait_time
FROM tb_by_client
WHERE status = #qualifier
GROUP BY Appt_date';
$rows = $db->fetchAll($sql);
$chartArray = [];
foreach($rows as $row){
$row->average = round($row->total_wait_time / $row->clients);
$chartObject = new stdClass();
$chartObject->date = $row->date;
$chartObject->average = $row->average;
$chartArray[] = $chartObject;
}
return json_encode($chartArray);

PHP - get value from JSON

Before i decode my JSON i get this result:
{
"1":[{"membership_id":1,"group_id":1,"user_id":1},
"2":[{"membership_id":3,"group_id":1,"user_id":2}
}
How would i specify that i want to select the one who has 'user_id' == 2 and return membership_id value?
My attempt, but i get undefined value 'user_id':
$myjson = json_decode($s_o, true);
foreach ($myjson as $key => $value){
if($value['user_id'] == $cid){
$mid = $value['membership_id'];
}
}
echo $mid;
Basically i guess i would first have to select the right object and go through it with the foreach, but here i got a bit lost in the situation.
Use Array-Functions:
$json = '{
"1":[{"membership_id":1,"group_id":1,"user_id":1}],
"2":[{"membership_id":3,"group_id":1,"user_id":2}]
}';
$array = json_decode($json, true);
$searchUserID = 2;
$filteredArray = array_filter($array, function($elem) use ($searchUserID){
return $searchUserID == $elem[0]['user_id'];
});
$mid = array_column(array_shift($filteredArray), 'membership_id')[0];
echo "Membership-ID: ".$mid;
array_filter uses a callback function that iterates over every element of the array. If the callback function returns true, that element is assigned to $filteredArray. No need for a foreach loop that way.
But the return value is the whole array element:
Array
(
[2] => Array
(
[0] => Array
(
[membership_id] => 3
[group_id] => 1
[user_id] => 2
)
)
)
So you have to extract your membership_id.
Read the following line from inside out.
First, we fetch the first entry of the array with array_shift (since we have only one entry, this will be our desired entry).
Array
(
[0] => Array
(
[membership_id] => 3
[group_id] => 1
[user_id] => 2
)
)
We pass this array on to array_column to find the entry in the encapsulated array with the column name membership_id. Since array_column again returns an array,
Array
(
[0] => 3
)
we get the (one and only) entry by adding [0] to the end of this command.
Since the last part is a little complicated, here's a torn apart version of it:
$firstEntryOfFilteredArray = array_shift($filteredArray);
$arrayWithValueOfColumnMembershipID = array_column($firstEntryOfFilteredArray, 'membership_id');
$membership_id = $arryWithValueOfColumnMembershipID[0];
These three lines are concatenated into this:
$mid = array_column(array_shift($filteredArray), 'membership_id')[0];
here's a working example: http://sandbox.onlinephpfunctions.com/code/8fe6ede71ca1e09dc68b2f3bec51743b27bf5303
I'm assuming the JSON actually looks like:
{
"1":[{"membership_id":1,"group_id":1,"user_id":1}],
"2":[{"membership_id":3,"group_id":1,"user_id":2}]
}
Each element of the object is an array for some reason. So you need to index it with $value[0] to access the object contained inside it.
$myjson = json_decode($s_o, true);
foreach ($myjson as $key => $value){
if($value[0]['user_id'] == $cid){
$mid = $value[0]['membership_id'];
break;
}
}
echo $mid;
If the arrays can contain multiple elements, you'll need nested loops.
$myjson = json_decode($s_o, true);
foreach ($myjson as $key => $value){
foreach ($value as $object) {
if($object['user_id'] == $cid){
$mid = $object['membership_id'];
break 2;
}
}
}
echo $mid;
This is a bit speculative, but I think the data is indexed by user ID. If that's the case, it makes the lookup much simpler.
After decoding with $myjson = json_decode($s_o, true);
Just find the record by ID and get the membership_id from the matching row.
$membership_id = reset($myjson['2'])['membership_id'];`
You should probably verify that that ID exists, so maybe something like:
$membership_id = isset($myjson['2']) ? reset($myjson['2'])['membership_id'] : null;
If I'm wrong and the fact that the row numbers match the user_id is just a coincidence, then never mind :)

Exclude the first result from an Array

I am currently messing around with iTunes Api and have ran into a problem with the returning process of albums track list returning the first result as the actual album data instead of the tracklist.
$loop['artist_name'] = $counted[$x]->artistName;
$loop['album_id'] = $counted[$x]->collectionId;
$loop['album_name'] = $counted[$x]->collectionName;
$loop['track_number'] = $counted[$x]->trackCount;
$loop['artwork_url'] = $counted[$x]->artworkUrl100;
$loop['copyright'] = $counted[$x]->copyright;
$loop['genre'] = $counted[$x]->primaryGenreName;
$loop['release_date'] = $counted[$x]->releaseDate;
$data_b = file_get_contents('https://itunes.apple.com/lookup?id='.$loop['album_id'].'&entity=song');
$response_ab = json_decode($data_b);
print '<pre>';
print_r($response_ab);
print '</pre>';
The above portion for print_r returns the following data.
[results] => Array
(
[0] => stdClass Object
(
[wrapperType] => collection
)
[1] => stdClass Object
(
[wrapperType] => track
)
Then continues onward down the track list, how can I remove the first [0] option from my loop? It appears this only returns the album name anyway when I am trying to get the tracklist.
I didn't test the below code however it should work.
$data_b = file_get_contents('https://itunes.apple.com/lookup?id='.$loop['album_id'].'&entity=song');
$response_ab = json_decode($data_b);
$count = $response_ab->results;
$arr = count($count);
for($a = 1; $a < $arr; $a++) {
// do some cool stuff here
}

How to muliply array values and store in third array variable in codeigniter

I have two array variables like this
$order_qty=array('1','2','3');
$quantity_per_pack= array('50','100','100')
I want to multiply these two variables and get stored in third variable which will be in array form such as
$total_order_qty = array('50','200','300')
This is my code:
for ($i=0;$i<10;$i++)
{
$total_order_qty[$i] = $quantity_per_pack[$i] * $order_qty[$i] ;
echo $total_order_qty[$i];
}
I declared these three variables as array before for loop.
Please help me to get solution for this.
Hope this will help your
you can use array_map for that like this
working demo :https://eval.in/1014627
function multi($n, $m)
{
return($n*$m);
}
$order_qty = array('1','2','3');
$quantity_per_pack = array('50','100','100');
$total_order_qty = array_map("multi", $order_qty, $quantity_per_pack);
print_r($total_order_qty );
Program Output
Array
(
[0] => 50
[1] => 200
[2] => 300
)
For more : http://php.net/manual/en/function.array-map.php

PHP - How to assign string key and value for array within 2 foreach loops?

The problem I have is that value gets overwritten in second foreach loop. I need to set the key to image thumbnail link and set value to an image path.
$img_thumbs = array('folder/thumb1.jpg','folder/thumb2.jpg','folder/thumb3.jpg');
$img_links = array('folder/image1.jpg','folder/image2.jpg','folder/image3.jpg');
$imgs = array();
foreach($img_links as $img_val)
{
foreach($img_thumbs as $thum_val)
{
$imgs[$thum_val] = $img_val
}
}
print_r($imgs);
OUTPUT (notice how image value repeats the last value):
Array (
["folder/thumb1.jpg"] => ["folder/image3.jpg"],
["folder/thumb2.jpg"] => ["folder/image3.jpg"],
["folder/thumb3.jpg"] => ["folder/image3.jpg"]
)
WHAT I NEED:
Array (
["folder/thumb1.jpg"] => ["folder/image1.jpg"],
["folder/thumb2.jpg"] => ["folder/image2.jpg"],
["folder/thumb3.jpg"] => ["folder/image3.jpg"]
)
Thanks in advance
$imgs = array_combine($img_thumbs, $img_links);
See http://php.net/array_combine
If you absolutely wanted to do that in a loop:
foreach ($img_thumbs as $i => $thumb) {
$imgs[$thumb] = $img_links[$i];
}
you just need to get rid of one of the foreach loops and increment the Images array by 1 each time you go through the loop

Categories