PHP - array_push is replacing instead of add to SESSION Variable - php

<?php
session_start();
$pid = $_GET['pid'];
$ptype = $_GET['ptype'];
$_SESSION = array();
$_SESSION['cart_items'] = array();
if (isset($_GET['add_cart']) && !empty($_GET['add_cart'])) {
// Add new data to Session var
$newdata = array($pid , $ptype, 1 );
array_push($_SESSION['cart_items'], $newdata);
}
echo '<pre>';
var_dump($_SESSION);
echo '</pre>';
?>
array_push replaces the data already in the $_SESSION with $newdata variable in the $_SESSION instead of adding it.
For example:
I enter the url: ?pid=1&ptype=CH-&add_cart=Add+to+Cart
And the array looks like this:
array(1) {
["cart_items"]=>
array(1) {
[0]=>
array(3) {
[0]=>
string(1) "1"
[1]=>
string(3) "CH-"
[2]=>
int(1)
}
}
}
That's great. But when I enter another url like: ?pid=1&ptype=CPU-&add_cart=Add+to+Cart
The array looks like this:
array(1) {
["cart_items"]=>
array(1) {
[0]=>
array(3) {
[0]=>
string(1) "1"
[1]=>
string(4) "CPU-"
[2]=>
int(1)
}
}
}
instead of this:
array(1) {
["cart_items"]=>
array(1) {
[0]=>
array(3) {
[0]=>
string(1) "1"
[1]=>
string(3) "CH-"
[2]=>
int(1)
}
[1]=>
array(3) {
[0]=>
string(1) "1"
[1]=>
string(4) "CPU-"
[2]=>
int(1)
}
}
}
It replaces the data that was already in the Session. I want it to add to it. How do I do so?
Thanks in advance!

change lines 5&6 from
$_SESSION = array();
$_SESSION['cart_items'] = array();
to
// $_SESSION = array();
// $_SESSION['cart_items'] = array();
array_push wasn't clearing your data. those two lines were clearing your session data on every page load.

Related

WSDL to Array php

A web service that I want to use to put data into an array in the project
I did not see a good answer.
This web service returns the info of a blog post, how data is in loop?
By writing these codes, I got the arrays in a string:
<?php
$client = new
SoapClient('http://service.test.com/test_information.asmx?wsdl');
$param = array('username' => 'admin','password' => 'admin','feature' =>
'SOAP_SINGLE_ELEMENT_ARRAYS');
$result = $client->GetInfoWeblog($param);
$outterArray = ((array)$result);
$innerArray = ((array)$outterArray['GetInfoWeblogResult']);
$dataArray = ((array)$innerArray['listObject']);
$array = json_decode(json_encode($dataArray), True);
echo '<pre>';
var_dump($array);
output
array(1) {
["OPMWebBlog"]=>
array(2) {
[0]=>
array(9) {
["WebVcCode"]=>
int(1)
["WebTitle"]=>
string(9) "Webtitle1"
["WebBody"]=>
string(8) "WebBody1"
["WebUrl"]=>
string(7) "Weburl1"
["WebDesc"]=>
string(8) "WebDesc1"
["WebDatetimeInsert"]=>
string(19) "2007-05-08T12:35:00"
["WebDatetimeUpdate"]=>
string(19) "2018-11-06T14:56:00"
["WebTag"]=>
string(8) "Web Tag1"
["WebPublishActive"]=>
bool(true)
}
[1]=>
array(9) {
["WebVcCode"]=>
int(2)
["WebTitle"]=>
string(9) "Webtitle2"
["WebBody"]=>
string(8) "WebBody2"
["WebUrl"]=>
string(7) "Weburl2"
["WebDesc"]=>
string(8) "WebDesc2"
["WebDatetimeInsert"]=>
string(19) "2018-11-06T14:56:00"
["WebDatetimeUpdate"]=>
string(23) "2007-05-08T12:35:29.123"
["WebTag"]=>
string(8) "Web Tag2"
["WebPublishActive"]=>
bool(true)
}
I want to get these objects in a loop
WebTitle
WebBody
WebUrl
WebDesc
WebDatetimeInsert
WebTag
WebPublishActive
I think if you loop through $array['OPMWebBlog'] you will get what you want :
foreach ($array['OPMWebBlog'] as $item) {
echo $item['WebTitle'];
echo $item['WebBody'];
...
}

How to get facebook album photos id and time using php?

I'm new in php. I make below two code to get facebook album photos id and create time. But its not working and don't showing me result/ID, only showing me blank page.
Here is my two code with $json and $array output.
Code 1;
<?php
$token="<token>";
$data = file_get_contents("https://graph.facebook.com/106097030098624/photos?fields=id&access_token=$token");
$json = json_decode($data);
echo $json->id;
echo $json->created_time;
?>
Code 1 Output: using var_dump($json);
object(stdClass)#1 (2) {
["data"]=>
array(3) {
[0]=>
object(stdClass)#2 (2) {
["id"]=>
string(15) "160246594547781"
["created_time"]=>
string(24) "2017-08-04T18:09:13+0000"
}
[1]=>
object(stdClass)#3 (2) {
["id"]=>
string(15) "160246581214449"
["created_time"]=>
string(24) "2017-08-04T18:09:12+0000"
}
[2]=>
object(stdClass)#4 (2) {
["id"]=>
string(15) "160246587881115"
["created_time"]=>
string(24) "2017-08-04T18:09:13+0000"
}
}
["paging"]=>
object(stdClass)#5 (1) {
["cursors"]=>
object(stdClass)#6 (2) {
["before"]=>
string(20) "MTYwMjQ2NTk0NTQ3Nzgx"
["after"]=>
string(20) "MTYwMjQ2NTg3ODgxMTE1"
}
}
}
Code 2:
<?php
$token="<token>";
$data = file_get_contents("https://graph.facebook.com/106097030098624/photos?fields=id&access_token=$token");
$array = json_decode($data, true);
echo $array['data']['id'];
echo $array['data']['created_time'];
?>
Code 2 Output: Using var_dump($array);
array(2) {
["data"]=>
array(3) {
[0]=>
array(2) {
["id"]=>
string(15) "160246594547781"
["created_time"]=>
string(24) "2017-08-04T18:09:13+0000"
}
[1]=>
array(2) {
["id"]=>
string(15) "160246581214449"
["created_time"]=>
string(24) "2017-08-04T18:09:12+0000"
}
[2]=>
array(2) {
["id"]=>
string(15) "160246587881115"
["created_time"]=>
string(24) "2017-08-04T18:09:13+0000"
}
}
["paging"]=>
array(1) {
["cursors"]=>
array(2) {
["before"]=>
string(20) "MTYwMjQ2NTk0NTQ3Nzgx"
["after"]=>
string(20) "MTYwMjQ2NTg3ODgxMTE1"
}
}
}
Please help me to solve this issue.thanks
For first one you need to do:-
<?php
$token="<token>";
$data = file_get_contents("https://graph.facebook.com/106097030098624/photos?fields=id&access_token=$token");
$json = json_decode($data);
foreach($array.data as $arr){
echo $arr.id; echo PHP_EOL; // you can use `echo "<br/>";`
echo $arr.created_time;echo PHP_EOL;// you can use `echo "<br/>";`
}
?>
For second-one you can use:-
<?php
$token="<token>";
$data = file_get_contents("https://graph.facebook.com/106097030098624/photos?
fields=id&access_token=$token");
$array = json_decode($data, true);
foreach($array['data'] as $arr){
echo $arr['id']; echo PHP_EOL; // you can use `echo "<br/>";`
echo $arr['created_time'];echo PHP_EOL;// you can use `echo "<br/>";`
}
?>
Output:- https://eval.in/841721

Converting Json String into PHP array then using php array

Im new to json & php and I'm having some issues with json into php string
My json string looks like this
{"status":"OK","cards":
[{"id":100001,"name":"batman","image":11111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T11:37:07Z"},
{"id":100002,"name":"superman","image":111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T12:30:09Z"},
{"id":100003,"name":"catwoman","image":1111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T12:39:42Z"},
{"id":100004,"name":"bane","image":1111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-09-08T12:56:04Z"}
]}
So Far i have created my string
$json_raw = '{"status":"OK","cards": [{"id":100001,"name": .....
Decoded the json
$arr = json_decode($json_raw, TRUE);
I var_dump($arr);
then it returns
array(2) { ["status"]=> string(2) "OK" ["cards"]=> array(4) { [0]=> array(8) { ["id"]=> int(100001) ["name"]=> string(6) "batman" ["image"]=> int(11111) ["size"]=> int(75) ["region_id"]=> int(1) ["locked"]=> bool(false) ["status"]=> string(6) "active" ["created_at"]=> string(20) "2013-08-15T11:37:07Z" } [1]=> array(8) { ["id"]=> int(100002) ["name"]=> string(8) "superman" ["image"]=> int(111111) ["size"]=> int(75) ["region_id"]=> int(1) ["locked"]=> bool(false) ["status"]=> string(6) "active" ["created_at"]=> string(20) "2013-08-15T12:30:09Z" } [2]=> array(8) { ["id"]=> int(100003) ["name"]=> string(8) "catwoman" ["image"]=> int(1111111) ["size"]=> int(75) ["region_id"]=> int(1) ["locked"]=> bool(false) ["status"]=> string(6) "active" ["created_at"]=> string(20) "2013-08-15T12:39:42Z" } [3]=> array(8) { ["id"]=> int(100004) ["name"]=> string(4) "bane" ["image"]=> int(1111111) ["size"]=> int(75) ["region_id"]=> int(1) ["locked"]=> bool(false) ["status"]=> string(6) "active" ["created_at"]=> string(20) "2013-09-08T12:56:04Z" } } }
Now all I want to do is be able to use this data
e.g if name = batman then
I know this is a stupid question but I am struggling :(
Thank in Advance
json_decode() with TRUE as second parameter gives you an associative array. You need to access the correct index to do what you want.
To list the complete associative array with nice formatting, you can do:
echo '<pre>', print_r($arr), '</pre>';
Now, to access the name in your array:
$man = $arr['cards'][0]['name'];
To check if it's Batman (yay!):
if( isset($man) && $man == 'batman' ) {
# code ...
}
For getting the name of all similar names:
$man = $json['cards']['0']['name'];
for ($i=0; $i < count($json['cards']); $i++) {
echo $json['cards'][$i]['name']."\n";
}
See it live!
when you got the array
$arr = json_decode($json_raw, TRUE);
then check if cards key exist
if(array_key_exists('cards', $arr)){
foreach($arr['cards'] as $key=>$val){
echo $key; ///name, id..
echo $val; /// batman,...
if($key == 'name' && $val =='batman'){
//-------do your stuff
}
}
}
Try with:
$cards = $arr['cards'];
foreach($cards as $card) {
if($card['name'] == 'batman') echo 'Hello batman!';
}
EDIT:
Ok, so this worked for me using code above, try it yourself if you want:
<?php
$json_raw = '{"status":"OK","cards":
[{"id":100001,"name":"batman","image":11111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T11:37:07Z"},
{"id":100002,"name":"superman","image":111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T12:30:09Z"},
{"id":100003,"name":"catwoman","image":1111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T12:39:42Z"},
{"id":100004,"name":"bane","image":1111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-09-08T12:56:04Z"}
]}';
$arr = json_decode($json_raw, TRUE);
$cards = $arr['cards'];
foreach($cards as $card) {
if($card['name'] == 'batman') echo 'Hello batman!';
}
?>

Push an associate array into another array

I am trying to do a custom WP_query loop and put the results into an array of associate arrays.. It is not working too well.. I realize the array_push overwrites any arrays that have the same indexes so I have the index increment +1 in the loop so they are not identical..however it still is not working.. My results show correct only on the first index (zero).. Here is my code:
<?php
$permlink='permalink';
$excerpt='exerpt';
$title='title';
$id='id';
$finalarray=array();
for ($i = 0; $i <= 10; $i++) {
$newitem = array(array(
'id'.$i =>$id.$i,
'title'.$i => $title.$i,
'excerpt'.$i => $excerpt.$i,
'permalink'.$i => $permlink.$i
));
array_push($finalarray, $newitem);
}
$count=0;
foreach($finalarray as $item){
echo $count.':'.'<br>';
echo $item[$count]['title'.$count];
echo $item[$count]['id'.$count];
echo $item[$count]['permalink'.$count];
$count++;
}
var_dump($finalarray);
?>
Any my results show :
0:
title0id0permalink01:
2:
3:
4:
5:
6:
7:
8:
9:
10:
array(11) { [0]=> array(1) { [0]=> array(4) { ["id0"]=> string(3) "id0" ["title0"]=> string(6) "title0" ["excerpt0"]=> string(7) "exerpt0" ["permalink0"]=> string(10) "permalink0" } } [1]=> array(1) { [0]=> array(4) { ["id1"]=> string(3) "id1" ["title1"]=> string(6) "title1" ["excerpt1"]=> string(7) "exerpt1" ["permalink1"]=> string(10) "permalink1" } } [2]=> array(1) { [0]=> array(4) { ["id2"]=> string(3) "id2" ["title2"]=> string(6) "title2" ["excerpt2"]=> string(7) "exerpt2" ["permalink2"]=> string(10) "permalink2" } } [3]=> array(1) { [0]=> array(4) { ["id3"]=> string(3) "id3" ["title3"]=> string(6) "title3" ["excerpt3"]=> string(7) "exerpt3" ["permalink3"]=> string(10) "permalink3" } } [4]=> array(1) { [0]=> array(4) { ["id4"]=> string(3) "id4" ["title4"]=> string(6) "title4" ["excerpt4"]=> string(7) "exerpt4" ["permalink4"]=> string(10) "permalink4" } } [5]=> array(1) { [0]=> array(4) { ["id5"]=> string(3) "id5" ["title5"]=> string(6) "title5" ["excerpt5"]=> string(7) "exerpt5" ["permalink5"]=> string(10) "permalink5" } } [6]=> array(1) { [0]=> array(4) { ["id6"]=> string(3) "id6" ["title6"]=> string(6) "title6" ["excerpt6"]=> string(7) "exerpt6" ["permalink6"]=> string(10) "permalink6" } } [7]=> array(1) { [0]=> array(4) { ["id7"]=> string(3) "id7" ["title7"]=> string(6) "title7" ["excerpt7"]=> string(7) "exerpt7" ["permalink7"]=> string(10) "permalink7" } } [8]=> array(1) { [0]=> array(4) { ["id8"]=> string(3) "id8" ["title8"]=> string(6) "title8" ["excerpt8"]=> string(7) "exerpt8" ["permalink8"]=> string(10) "permalink8" } } [9]=> array(1) { [0]=> array(4) { ["id9"]=> string(3) "id9" ["title9"]=> string(6) "title9" ["excerpt9"]=> string(7) "exerpt9" ["permalink9"]=> string(10) "permalink9" } } [10]=> array(1) { [0]=> array(4) { ["id10"]=> string(4) "id10" ["title10"]=> string(7) "title10" ["excerpt10"]=> string(8) "exerpt10" ["permalink10"]=> string(11) "permalink10" } } }
So it looks like the values are in the array, however, only the first index ( zero ) prints correctly.. any suggestions? Also, is there any way i can push an associate array and it not be over written so I dont have to increment the index?
There are a few problems here.
for ($i = 0; $i <= 10; $i++) {
$newitem = array(array(
'id'.$i =>$id.$i,
'title'.$i => $title.$i,
'excerpt'.$i => $excerpt.$i,
'permalink'.$i => $permlink.$i
));
array_push($finalarray, $newitem);
}
You are pushing an array, containing an array, containing 4 elements into $finalarray. There is no need for the outer array(). You can just do $newitem = array(...). array_push appends to end of the array, incrementing the index for you.
Second, in your foreach, your $count variable is not needed at all. When using foreach, $item is the element of the array. No need to look it up by the index.
If you want the index, however, foreach can give it to you.
foreach($finalarray as $count=>$item){
echo $count.':'.'<br>';
echo $item['title'.$count];
echo $item['id'.$count];
echo $item['permalink'.$count];
}
Your original code wasn't working because in each $item there was one array. That array had one item. In each element, the inner aray was always at index 0. Incrementing $count made it so only the 1st element worked, the others didn't because $item[1] didn't exist, it was always $item[0].
for ($i = 0; $i <= 10; $i++) {
$newitem = array(array(
'id'.$i =>$id.$i,
'title'.$i => $title.$i,
'excerpt'.$i => $excerpt.$i,
'permalink'.$i => $permlink.$i
));
$finalarray[] = $newitem;
unset($newitem);
}
Since you don't care about the key of the final array just add it on using $finalarray[]

Add Array Column to Multidimensional Array Using array_merge in foreach Loop

I'm trying to add another column of data to each row in a foreach loop. It's purpose is to remember the element of data importeded from XML processed to an multidimensional array. It's stuck as a scalar though the var_dumps looks fine.
<?php
$KEY = 0;
foreach ($eventsArray as $keyMe){
$thisKey['KEY'][0] = strval($KEY);
$keyedArray = array_merge($keyMe, $thisKey);
$KEY++;
}
// Prep for multisort
foreach ($keyedArray as $key => $value){
$date[$key] = $value['DATE'];
$title[$key] = $value['TITLE'];
$link[$key] = $value['LINK'];
$slide[$key] = $value['SLIDE'];
$location[$key] = $value['LOCATION'];
$time[$key]= $value['TIME'];
$KEY[$key] = $value['KEY']; // Warning: Cannot use a scalar value as an array
}
/* var_dump(
array(7) {
["DATE"]=> array(1) { [0]=> string(10) "2012-12-18" }
["TITLE"]=> array(1) { [0]=> string(20) "Event Title" }
["LINK"]=> array(1) { [0]=> string(38) "aLinkLocation.htm" }
["SLIDE"]=> array(1) { [0]=> string(2) "16" }
["LOCATION"]=> array(1) { [0]=> string(8) "Location of Event" }
["TIME"]=> array(1) { [0]=> string(3) "8am" }
["KEY"]=> array(1) { [0]=> string(2) "23" }
}
*/

Categories