PHP Sessions and Form input names getting confused [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am seeing some weird behavior:
I have 2 sessions that I set with some value: $_SESSION['shippingZip'] and $_SESSION['shippingOption'].
Then my code goes through this code to post the input values sent in a form:
$shippingOption = $_POST['shippingOption'];
Print_r ($_SESSION);
$shippingZip = $_POST['shippingZip'];
Those POSTs are coming empty in this pass. However, the Print shows my session $_SESSION['shippingOption'] empty, when it should show the string that had previously been assigned to it.
-------------------------- POSTING FULL PROOF
Sessions are loaded with some data:
$_SESSION['shippingOption'] = $shippingOption;
$_SESSION['shippingZip']= $shippingZip;
Then:
Print_r ($_SESSION);
$shippingOption = $_POST['shippingOption'];
Print_r ($_SESSION);
$shippingZip = $_POST['shippingZip'];
Print_r ($_SESSION);
Output:
Array ( [itemAdded] => 1 [Payment_Amount] => 46.52 [cart] => Array ( [4] => Array ( [itemId] => 4 [qty] => 1 ) ) [shippingOption] => FIRST CLASS [shippingZip] => 10025 [shippingPrice] => 1.52 )
Array ( [itemAdded] => 1 [Payment_Amount] => 46.52 [cart] => Array ( [4] => Array ( [itemId] => 4 [qty] => 1 ) ) [shippingOption] => [shippingZip] => 10025 [shippingPrice] => 1.52 )
Array ( [itemAdded] => 1 [Payment_Amount] => 46.52 [cart] => Array ( [4] => Array ( [itemId] => 4 [qty] => 1 ) ) [shippingOption] => [shippingZip] => [shippingPrice] => 1.52
You can clearly see how after each POST, the SESSION with the same name loses its value. It's totally crazy!!!

$_POST does not directly populate $_SESSION. You need to assign the values to the session
i.e.
$_SESSION['shippingOption'] = $_POST['shippingOption'];
EDIT
After you posted more code, it looks like you are not defining $shippingOption; before setting it $_SESSION['shippingOption'] = $shippingOption;
Make sure the order goes like this:
$shippingOption = $_POST['shippingOption'];
$_SESSION['shippingOption'] = $shippingOption;

Related

Parse JSON with PHP - irregular format [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
New to php and trying to figure out how to parse API data which is returned in what looks like a weird format. Here is a sample of the data:
[{"campaign_id":"9000","date":"2016-01-11","totalcount":"1838","page":"1","totalpages":1,"index":1,"count":1838},{"video2.stack.com":["84254","105","0","83.71"],...,"zierfischforum.at":["1","0","0","0.00"]}]
Here is an example of how you can parse your JSON as an array:
$json_string = '[{"campaign_id":"9000","date":"2016-01-11","totalcount":"1838","page":"1","totalpages":1,"index":1,"count":1838},{"video2.stack.com":["84254","105","0","83.71"],"zierfischforum.at":["1","0","0","0.00"]}]';
$json_array = json_decode($json_string, true); // true gets us an array
echo '<pre>';
print_r($json_array);
echo $json_array[1]['video2.stack.com'][0];
Provides the following results:
Array
(
[0] => Array
(
[campaign_id] => 9000
[date] => 2016-01-11
[totalcount] => 1838
[page] => 1
[totalpages] => 1
[index] => 1
[count] => 1838
)
[1] => Array
(
[video2.stack.com] => Array
(
[0] => 84254
[1] => 105
[2] => 0
[3] => 83.71
)
[zierfischforum.at] => Array
(
[0] => 1
[1] => 0
[2] => 0
[3] => 0.00
)
)
)
84254
First we output the entire array. Based on data there we are able to single out a value for one of the array parts for video2.stack.com. It is relatively easy to traverse and you should be able extract any information you need. You could even build a recursive search function for your JSON.
NOTE: I removed some of your data(the part ,...) as it made your JSON non-valid.

PHP Multi-dimensional array rearranging [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have the following array that i want to re-arrange
Array
(
[0] => stdClass Object
(
[feeds_id] => 1338
[flag] => 0
)
[1] => stdClass Object
(
[feeds_id] => 1339
[flag] => 0
)
[2] => stdClass Object
(
[feeds_id] => 1339
[flag] => 1
)
)
I want to arrange it to look like this
[1338] => Array (
[0] => 0
)
[1339] => Array (
[0] => 0
[1] => 1
)
This code should work:
$newArray=array();
foreach($items as $item){
if(!is_array($newArray[$item->feeds_id])){
$newArray[$item->feeds_id]=array();
}
array_push($newArray[$item->feeds_id],$item->flag);
}
You should first create an empty array where the new data will be stored. Then, inside the foreach, you should use array_push, BUT if the sub-array in where you want to put the data is not an array, you should declare it first (that's why the "if" before the array_push)

Can't echo out first item from array [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Using PHP I am try to echo out the first item from an array...
Array
(
[docs] => Array
(
[0] => Array
(
[imgurl] => http://www.example.com/image1.jpg
)
[1] => Array
(
[imgurl] => http://www.example.com/image2.jpg
)
[2] => Array
(
[imgurl] => http://www.example.com/image3.jpg
)
[3] => Array
(
[imgurl] => http://www.example.com/image4.jpg
)
)
)
I am using the following PHP to attempt to display the first item...
echo $array['docs'][0]['imgurl'];
But it is giving me the error...
Warning: Illegal string offset 'docs'
Can anyone show me what I am doing wrong?
#fightstarr20, Your array format is not correct. I just correct it and then tried and it works fine.
<?php
$array = Array('docs' => Array
(
0 => Array
(
'imgurl' => 'http://www.example.com/image1.jpg'
),
1 => Array
(
'imgurl' => 'http://www.example.com/image2.jpg'
),
2 => Array
(
'imgurl' => 'http://www.example.com/image3.jpg'
),
3 => Array
(
'imgurl' => 'http://www.example.com/image4.jpg'
),
)
);
echo $array['docs'][0]['imgurl'];
?>
Output is:- http://www.example.com/image1.jpg
Since you have not given the full code I assume you have the following array:
$array = array('docs'=>array(
'0'=>array('imgurl'=>'http://www.example.com/image0.jpg'),
'1'=>array('imgurl'=>'http://www.example.com/image1.jpg'),
'2'=>array('imgurl'=>'http://www.example.com/image2.jpg'),
'3'=>array('imgurl'=>'http://www.example.com/image3.jpg'),
));
Then you can access the imgurl as:
echo $array['docs'][0]['imgurl'];
You can test the above code here

How can I print or get a specific value on an associative array (PHP) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an array that returns keys and values.
<?php
print_r(array_values($arResult["DISPLAY_PROPERTIES"]));
$arresults = $arResult["DISPLAY_PROPERTIES"];
?>
The code above would show this result:
Array ([0] => Array
([ID] => 29
[TIMESTAMP_X] => 2014-08-26 08:23:13
[IBLOCK_ID] => 3
[NAME] => ProductID
[ACTIVE] => Y
[SORT ] => 500
[CODE] => product_id
[DEFAULT_VALUE] =>
[PROPERTY_TYPE] => S
[ROW_COUNT] => 1
[COL_COUNT] => 30
[LIST_TYPE] => L
[MULTIPLE] => N
[XML_ID] => [file_type] =>
[MULTIPLE_CNT] => 5
[TMP_ID] =>
[LINK_IBLOCK_ID] => 0
[WITH_DESCRIPTION] => N
[SEARCHABLE] => N
[filtrable] => N
[IS_REQUIRED] => N
[VERSION] = > 1
[user_type] =>
[USER_TYPE_SETTINGS] =>
[HINT] =>
[PROPERTY_VALUE_ID] => 11880
[VALUE] => PCA.CD08.21.A1
[DESCRIPTION] =>
[VALUE_ENUM] =>
[VALUE_XML_ID] =>
[VALUE_SORT] =>
[~ VALUE] => PCA.CD08.21.A1
[~ DESCRIPTION] =>
[~ NAME] => ProductID
[~ DEFAULT_VALUE] =>
[display_value] => PCA.CD08.21.A1)
)
Problem: How can I show the value PCA.CD08.21.A1 .. What php code do i use to display only PCA.CD08.21.A1 ?
Kindly Please Help Me...
You can simply access the value directly:
echo $arResult['DISPLAY_PROPERTIES'][0]['VALUE'];
However, you have to replace index 0 with the proper index, since calling array_values() on $arResult will numerically reindex it.
foreach($arResult["DISPLAY_PROPERTIES"] as $key => $value)
{
if($key == 'VALUE')
echo $value;
}
Is this what you need?
$display_value = $arResult["DISPLAY_PROPERTIES"][0]["display_value"]
Keeping in mind that we do not know what the first key in the array is you can do the following:
$arr = array_shift($arResult["DISPLAY_PROPERTIES"]);
$display_value = $arr["display_value"];

How can I merge two arrays into one [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I've looked on this site and have found a few examples of merging arrays but to be honest, I'm not sure which one is right for me.
I am having a difficult time trying to merge these arrays. I've tried array_combine and array_merge but I am not having any luck.
What I need is a single array with the output like this:
Array
(
[0] => Array
(
[UnitNo] => 91
[Name] => Receiving
[ActivityNo] =>
[Active] => 2
[CallNo] =>
[CallStatusNo] =>
[Assigned] =>
[UnitId] => 2
)
[1] => Array
(
[UnitNo] => 83
[Name] => Shipping
[ActivityNo] =>
[Active] =>
[CallNo] =>
[CallStatusNo] =>
[Assigned] =>
[UnitId] => 1
)
)
These are the two arrays that I need to merge together based on the UnitId that should form a complete array like the one above. I just don't know how to do this. If someone could direct me a bit, I would be grateful.
// Array #1
Array
(
[0] => Array
(
[UnitId] => 2
[UnitNo] => 91
[Name] => Receiving
[Active] => 4
)
[1] => Array
(
[UnitId] => 1
[UnitNo] => 83
[Name] => Shipping
[Active] => 4
)
)
// Array #2
Array
(
[0] => Array
(
[UnitId] => 2
[ActivityNo] => 1
[CallNo] => 1
[CallStatusNo] => 1
[Assigned] => 1
)
[1] => Array
(
[UnitId] => 1
[ActivityNo] => 11
[CallNo] => 2
[CallStatusNo] => 1
[Assigned] => 1
)
)
This isn't a case where you can just use a php function and call it good - you will have to do some custom logic of your own.
This would work, but only if both $array1 and $array2 have the same items at each index.
$array1;
$array2;
$mergedArray;
for ($i=0; $i++; $i < count($array1) - 1) {
//first add all the values from array1
$mergedArray[$i] = $array1[$i];
foreach($array2[$i] as $array2Key=>$array2Value){
//now check for missing key/values that are in array2 but not in array1
if (array_key_exists($array2Key, $array1[$i]) == false) {
// the key does not exist in array1, so add it to mergedArray
$mergedArray[$i][$array2Key] = $array2Value;
}
}
}
It would be safer if your arrays were associative arrays, indexed by a unique value.

Categories