Incomplete Data from json_decode - php

Okay some basic info...
PHP 5.3.5
JSON 1.2.1
This is all running on a local XAMPP server. Basically, my code below is returning an incomplete associative array. The json_last_error() returns 0. I have searched the net but could not find this type of error without haveing a specific json error returned.
<?php
$ShippingObj = json_decode($_POST['formData'], true);
echo json_last_error();
echo $ShippingObj;
I have validated the string with JSONLint..and here is what its output was....
array (
'name' => 'newShipment',
'comments' => '',
'count' => 1,
'itemID' => 0,
'itemWeight' => 7.548,
'itemLength' => 0,
'itemWidth' => 0,
'itemHeight' => 0,
'limits' => '',
'collection' =>
array (
0 =>
array (
'name' => '8x6x4 Standard',
'comments' => '',
'count' => 5,
'itemID' => '3',
'itemWeight' => 7.5475,
'itemLength' => '8',
'itemWidth' => '6',
'itemHeight' => '4',
'limits' => 'MFR',
'collection' =>
array (
0 =>
array (
'ID' => '1',
'Name' => ' TA-200 Demarcation Enclosure',
'Qty' => 1,
'Weight' => '0.34',
),
1 =>
array (
'ID' => '2',
'Name' => ' CG-1000 Demarcation Enclosure',
'Qty' => 4,
'Weight' => '1',
),
2 =>
array (
'ID' => '19',
'Name' => ' FM-02 Stereo FM Transmitter',
'Qty' => 1,
'Weight' => '0.1625',
),
3 =>
array (
'ID' => '20',
'Name' => ' Renard 64 Rev XC Parts Kit w/PCB',
'Qty' => 1,
'Weight' => '1.125',
),
4 =>
array (
'ID' => '21',
'Name' => ' SSRez Parts Kit w/PCB - Set of 8',
'Qty' => 2,
'Weight' => '0.86',
),
),
),
),
)
Which is a valid JSON construct.....here is the input that I copied into http://www.functions-online.com/json_decode.html to verify my string.
$returnValue = json_decode('{"name":"newShipment","comments":"","count":1,"itemID":0,"itemWeight":7.548,"itemLength":0,"itemWidth":0,"itemHeight":0,"limits":"","collection":{"0":{"name":"8x6x4 Standard","comments":"","count":5,"itemID":"3","itemWeight":7.5475,"itemLength":"8","itemWidth":"6","itemHeight":"4","limits":"MFR","collection":{"0":{"ID":"1","Name":" TA-200 Demarcation Enclosure","Qty":1,"Weight":"0.34"},"1":{"ID":"2","Name":" CG-1000 Demarcation Enclosure","Qty":4,"Weight":"1"},"2":{"ID":"19","Name":" FM-02 Stereo FM Transmitter","Qty":1,"Weight":"0.1625"},"3":{"ID":"20","Name":" Renard 64 Rev XC Parts Kit w/PCB","Qty":1,"Weight":"1.125"},"4":{"ID":"21","Name":" SSRez Parts Kit w/PCB - Set of 8","Qty":2,"Weight":"0.86"}}}}}', true);
The first section of code returns the below for $ShippingObj. Note the "[collection] => array(5) " that does not expand out...its empty. I have checked the string from the previous JS and it is fine.
array(10) (
[name] => (string) newShipment
[comments] => (string)
[count] => (int) 1
[itemID] => (int) 0
[itemWeight] => (float) 16.2955
[itemLength] => (int) 0
[itemWidth] => (int) 0
[itemHeight] => (int) 0
[limits] => (string)
[collection] => array(1) (
[0] => array(10) (
[name] => (string) CG2000 OEM Box
[comments] => (string)
[count] => (int) 5
[itemID] => (string) 2
[itemWeight] => (float) 8.1475
[itemLength] => (string) 21
[itemWidth] => (string) 16.5
[itemHeight] => (string) 12.5
[limits] => (string) 2ND
[collection] => array(5) (
)
)
)
)

Related

Trouble getting object from parsed JSON output

I'm playing around with the twitter API and i'm trying to parse the json output. However somehow i'm doing something wrong and I'm not getting what I would like to get.
In this case i'm writing in PHP. Started looping thru the array and screen_name and text are working accordingly.
foreach ($statuses as $obj) {
$screen_name = filter_var($obj->user->screen_name, FILTER_SANITIZE_STRING);
$text = filter_var($obj->text, FILTER_SANITIZE_STRING);
$urls = filter_var($obj->entities->urls->url, FILTER_SANITIZE_URL);
}
the JSON array output:
array (
0 =>
stdClass::__set_state(array(
'created_at' => 'Fri Oct 07 15:31:01 +0000 2016',
'text' => 'test',
'entities' =>
stdClass::__set_state(array(
'hashtags' =>
array (
0 =>
stdClass::__set_state(array(
'text' => '30Under30',
'indices' =>
array (
0 => 36,
1 => 46,
),
)),
),
'symbols' =>
array (
),
'user_mentions' =>
array (
),
'urls' =>
array (
0 =>
stdClass::__set_state(array(
'url' => 'https://Forbes.com',
'expanded_url' => 'https://twitter.com/i/web/status/784415744427687936',
'display_url' => 'twitter.com/i/web/status/7…',
'indices' =>
array (
0 => 117,
1 => 140,
),
)),
),
)),
'source' => 'Sprinklr',
'in_reply_to_screen_name' => NULL,
'user' =>
stdClass::__set_state(array(
'id' => 91478624,
'screen_name' => 'test',
'url' => 'http://Forbes.com',
'entities' =>
stdClass::__set_state(array(
'url' =>
stdClass::__set_state(array(
'urls' =>
array (
0 =>
stdClass::__set_state(array(
'url' => 'http://Forbes.com',
'expanded_url' => 'http://forbes.com',
'display_url' => 'forbes.com',
'indices' =>
array (
0 => 0,
1 => 22,
),
)),
),
)),
'description' =>
stdClass::__set_state(array(
'urls' =>
array (
0 =>
stdClass::__set_state(array(
'url' => 'http://Forbes.com',
'expanded_url' => 'http://Forbes.com',
'display_url' => 'Forbes.com',
'indices' =>
array (
0 => 28,
1 => 48,
),
)),
),
)),
)),
'protected' => false,
'notifications' => false,
)),
'geo' => NULL,
'lang' => 'en',
)),
)
Since urls is an array, you need to index it.
$urls = filter_var($obj->entities->urls[0]->url, FILTER_SANITIZE_URL);

Shopify orders array issue

I have these two arrays
$order2 = array('line_items' => array(
array('title' => 'Shipping',
'price' => '10',
'quantity' => '2',
)
));
$order3 =array('title' => 'Handling',
'price' => '5',
'quantity' => '1',
);
I've used array_merge and array_push in this instance but both don't give me my desired output. I'm trying to get it to show like this.
array('line_items' => array(
array('title' => 'Shipping',
'price' => '10',
'quantity' => '2',
),
array('title' => 'Handling',
'price' => '5',
'quantity' => '1',
)
));
The output i get is
Array ( [line_items] => Array ( [0] => Array ( [title] => Shipping [price] => 10 [quantity] => 2 ) ) [title] => Handling [price] => 5 [quantity] => 1 )
How can i achieve my desired output so i can use this as a shopify order?
You don't need any functions to do that, just a simple array assignment on top of the existing one:
$order2['line_items'][] = $order3;

PHP - generate multi-dimentional array

This is my array:
$arr = array(
0 => array(
'title' => 'test1',
'count' => 4,
'month' => 'jan-2015'
),
1 => array(
'title' => 'test2',
'count' => 10,
'month' => 'jan-2015'
),
2 => array(
'title' => 'test3',
'count' => 14,
'month' => 'jun-2015'
),
3 => array(
'title' => 'test4',
'count' => 45,
'month' => 'july-2015'
),
);
I've to convert this array into multi-dimentional array as below:
$arr = array(
'jan-2015' => array(
0 => array(
'title' => 'test1',
'count' => 4,
),
1 => array(
'title' => 'test2',
'count' => 10,
),
),
'jun-2015' => array(
0 => array(
'title' => 'test3',
'count' => 14,
),
),
'july-2015' => array(
0 => array(
'title' => 'test4',
'count' => 45,
),
),
);
I've tried to make it as expected but unfortunately i can't make this.
Is any other solutions for this?
According to your data structure :
$arr = array(
0 => array(
'title' => 'test1',
'count' => 4,
'month' => 'jan-2015'
),
1 => array(
'title' => 'test2',
'count' => 10,
'month' => 'jan-2015'
),
2 => array(
'title' => 'test3',
'count' => 14,
'month' => 'jun-2015'
),
3 => array(
'title' => 'test4',
'count' => 45,
'month' => 'july-2015'
),
);
try this:
$newArray = array();
foreach($arr as $key => $val) {
$newArray[$val['month']][] = $val;
}
echo '<pre>'.print_r($newArray,1).'</pre>';
Output:
Array
(
[jan-2015] => Array
(
[0] => Array
(
[title] => test1
[count] => 4
[month] => jan-2015
)
[1] => Array
(
[title] => test2
[count] => 10
[month] => jan-2015
)
)
[jun-2015] => Array
(
[0] => Array
(
[title] => test3
[count] => 14
[month] => jun-2015
)
)
[july-2015] => Array
(
[0] => Array
(
[title] => test4
[count] => 45
[month] => july-2015
)
)
)
You could use this function:
function transform($input) {
// Extract months, and use them as keys, with value set to empty array
// The array_fill_keys also removes duilicates
$output = array_fill_keys(array_column($input, 'month'), array());
foreach ($input as $element) {
$copy = $element;
// remove the month key
unset($copy["month"]);
// assign this to the month key in the output
$output[$element["month"]][] = $copy;
}
return $output;
}
Call it like this:
$arr = array(
0 => array(
'title' => 'test1',
'count' => 4,
'month' => 'jan-2015'
),
1 => array(
'title' => 'test2',
'count' => 10,
'month' => 'jan-2015'
),
2 => array(
'title' => 'test3',
'count' => 14,
'month' => 'jun-2015'
),
3 => array(
'title' => 'test4',
'count' => 45,
'month' => 'july-2015'
),
);
print_r (transform($arr));
Output:
Array
(
[jan-2015] => Array
(
[0] => Array
(
[title] => test1
[count] => 4
)
[1] => Array
(
[title] => test2
[count] => 10
)
)
[jun-2015] => Array
(
[0] => Array
(
[title] => test3
[count] => 14
)
)
[july-2015] => Array
(
[0] => Array
(
[title] => test4
[count] => 45
)
)
)
By using answer of #Girish Patidar, You can achieve this by:
$outputArr = array();
$to_skip = array();
foreach($arr as $row){
$to_skip = $row;
unset($to_skip['month']);
$outputArr[$row['month']][] = $to_skip;
}
echo "<pre>";
print_r($outputArr);
die;
There could many way to do this. Please try this one if it works for you
<?php
$newArr=NULL;
foreach($arr as $array)
{
$temp=NULL;
$temp['title']=$array['title'];
$temp['count']=$array['count'];
$newArr[$array['month']][]=$temp;
}
var_dump($newArr);
?>

PHP merge associative arrays by replacing same keys and adding new

I have an array of variable size structured like this (categories is only one of the keys inside data):
print_r($json[123]["data"]["categories"]);
array(
array(
'id' => '2',
'description' => 'Single-player'
),
array(
'id' => '1',
'description' => 'Multi-player'
),
array(
'id' => '9',
'description' => 'Co-op'
),
array(
'id' => '22',
'description' => 'Steam Achievements'
),
array(
'id' => '28',
'description' => 'Full controller support'
)
)
print_r($json[456]["data"]["categories"]);
array(
array(
'id' => '21',
'description' => 'Downloadable Content'
),
array(
'id' => '1',
'description' => 'Multi-player'
)
)
Now, I want to merge these sub-arrays (they can be in variable number) and have all keys added and replaced. I've tried array_merge but it replaces the keys without adding new ones.
In this case I need to obtain this array:
print_r($merged["data"]["categories"]);
array(
array(
'id' => '2',
'description' => 'Single-player'
),
array(
'id' => '1',
'description' => 'Multi-player'
),
array(
'id' => '9',
'description' => 'Co-op'
),
array(
'id' => '22',
'description' => 'Steam Achievements'
),
array(
'id' => '28',
'description' => 'Full controller support'
),
array(
'id' => '21',
'description' => 'Downloadable Content'
)
)
Any help?
Edit:
I think I didn't expressed myself well enough. $json[$id]["data"] has multiple keys I want to merge (categories is just an example). Also the number of $json[$id] keys is variable
Edit2:
The arrays can have duplicate values, and the depth of the keys can be variable. I need to get something like array_merge_recursive() but with same values replaced.
Edit3:
This is the current array. http://pastebin.com/7x7KaAVM I need to merge all keys that have sub-arrays
Try below code:
$json = array(
'123' => array('data' => array('categories' => array(
array(
'id' => '2',
'description' => 'Single-player'
),
array(
'id' => '1',
'description' => 'Multi-player'
),
array(
'id' => '9',
'description' => 'Co-op'
),
array(
'id' => '22',
'description' => 'Steam Achievements'
),
array(
'id' => '28',
'description' => 'Full controller support'
)
))
),
'456' => array('data' => array('categories' => array(
array(
'id' => '21',
'description' => 'Downloadable Content'
)
))
),
);
//print_r($json);
$merged = array();
foreach($json as $j1)
{
foreach($j1 as $j2)
{
foreach($j2 as $key => $j3)
{
foreach($j3 as $j4)
{
$merged[$key][] = $j4;
}
}
}
}
print_r($merged);
Result:
Array
(
[categories] => Array
(
[0] => Array
(
[id] => 2
[description] => Single-player
)
[1] => Array
(
[id] => 1
[description] => Multi-player
)
[2] => Array
(
[id] => 9
[description] => Co-op
)
[3] => Array
(
[id] => 22
[description] => Steam Achievements
)
[4] => Array
(
[id] => 28
[description] => Full controller support
)
[5] => Array
(
[id] => 21
[description] => Downloadable Content
)
)
)
Demo:
http://3v4l.org/X61bE#v430
Try this . To generalize I have added some more arrays.
<?php
$merged_array = array();
$final_array = array();
$json[123]["data"]["categories"] = array(
array(
'id' => '2',
'description' => 'Single-player'
),
array(
'id' => '1',
'description' => 'Multi-player'
),
array(
'id' => '9',
'description' => 'Co-op'
),
array(
'id' => '22',
'description' => 'Steam Achievements'
),
array(
'id' => '28',
'description' => 'Full controller support'
)
);
$json[456]["data"]["categories"] = array(
array(
'id' => '21',
'description' => 'Downloadable Content'
)
);
$json[786]["data"]["categories"] = array(
array(
'id' => '31',
'description' => 'Downloadable Content'
)
);
$json[058]["data"]["categories"] = array(
array(
'id' => '41',
'description' => 'Downloadable Content'
)
);
foreach($json as $key=>$value){
array_push($merged_array,$json[$key]["data"]["categories"]);
}
foreach($merged_array as $value){
foreach($value as $val){
array_push($final_array,$val);
}
}
print_r($final_array);
?>
RESULT
Array
(
[0] => Array
(
[id] => 2
[description] => Single-player
)
[1] => Array
(
[id] => 1
[description] => Multi-player
)
[2] => Array
(
[id] => 9
[description] => Co-op
)
[3] => Array
(
[id] => 22
[description] => Steam Achievements
)
[4] => Array
(
[id] => 28
[description] => Full controller support
)
[5] => Array
(
[id] => 21
[description] => Downloadable Content
)
[6] => Array
(
[id] => 31
[description] => Downloadable Content
)
[7] => Array
(
[id] => 41
[description] => Downloadable Content
)
)

How separate php array with same value and not same

I have a shop basket with for example 6 items. Some items are for one shop and another items for one and maybe two shops.
I want create factors for each shops. How can I do? How can I known these items in basket are for one shop or has for 2 or 3 shop . How can separate this items to factor.
Array('customer' => Array('basket' => Array(
'9_2' => Array
(
"row" => "0",
'item' => 'cd',
'count' => '1',
'sale_start_date' => '1391-12-25 19:27:56',
'sale_end_date' => '1392-04-20 19:27:49',
'sale_price' => '40500',
'price' => '54564',
'id' => '999035',
'shopid' => '4'
),
'999_17' => Array
(
'row' => '1',
'item' => 'car',
'count' => '1',
'sale_start_date' => '0000-00-00 00:00:00',
'sale_end_date' => '0000-00-00 00:00:00',
'sale_price' => '0',
'price' => '520000',
'id' => '999039',
'code' => 'b125nh',
'shopid' => '6'
),
'9_3' => Array
(
'row' => '2',
'item' => 'book',
'count' => '1',
'sale_start_date' => '0000-00-00 00:00:00',
'sale_end_date' => '0000-00-00 00:00:00',
'sale_price' => '0',
'price' => '520000',
'id' => '999039',
'code' => 'b125nh',
'shopid' => '4'
),
'10_5' => Array
(
'row' => '2',
'item' => 'dvd',
'count' => '1',
'sale_start_date' => '0000-00-00 00:00:00',
'sale_end_date' => '0000-00-00 00:00:00',
'sale_price' => '0',
'price' => '520000',
'id' => '999039',
'code' => 'b125nh',
'shopid' => '5'
)
)
)
);
This example is array from my SESSION . I want seperate factor with shopid value. In this basket for example we have 2 item from shopid=4 and have 1 item from shopid=6 and one item from shopid=5 how can create seprate factor for each shop.
First I use usort function for sort array with shopid but I can't continue for separate each same and not same item shop for create factor
Something like this should work
<?php
$shopItems = array();
foreach ($_SESSION['customer']['basket'] as $basket) {
$key = $basket['shopid'];
$shopItems[$key][] = $basket;
}
This will store groups of items from the same shop id in elements of the array $shopItems
For example (only showing shop id 4)
Array (
[4] => Array (
[0] => array(
"row" => "0",
'item' => 'cd',
'count' => '1',
'sale_start_date' => '1391-12-25 19:27:56',
'sale_end_date' => '1392-04-20 19:27:49',
'sale_price' => '40500',
'price' => '54564',
'id' => '999035',
'shopid' => '4'
),
[1] => array(
'row' => '2',
'item' => 'book',
'count' => '1',
'sale_start_date' => '0000-00-00 00:00:00',
'sale_end_date' => '0000-00-00 00:00:00',
'sale_price' => '0',
'price' => '520000',
'id' => '999039',
'code' => 'b125nh',
'shopid' => '4'
)
)
)
$basket = array();
foreach ($_SESSION['customer']['basket'] as $k => $v)
$basket[$v['shopid']][$k] = $v;
print_r($basket);
Result:
Array
(
[4] => Array
(
[9_2] => Array
(
[row] => 0
[item] => cd
[count] => 1
[sale_start_date] => 1391-12-25 19:27:56
[sale_end_date] => 1392-04-20 19:27:49
[sale_price] => 40500
[price] => 54564
[id] => 999035
[shopid] => 4
)
[9_3] => Array
(
[row] => 2
[item] => book
[count] => 1
[sale_start_date] => 0000-00-00 00:00:00
[sale_end_date] => 0000-00-00 00:00:00
[sale_price] => 0
[price] => 520000
[id] => 999039
[code] => b125nh
[shopid] => 4
)
)
[6] => Array
(
[999_17] => Array
(
[row] => 1
[item] => car
[count] => 1
[sale_start_date] => 0000-00-00 00:00:00
[sale_end_date] => 0000-00-00 00:00:00
[sale_price] => 0
[price] => 520000
[id] => 999039
[code] => b125nh
[shopid] => 6
)
)
[5] => Array
(
[10_5] => Array
(
[row] => 2
[item] => dvd
[count] => 1
[sale_start_date] => 0000-00-00 00:00:00
[sale_end_date] => 0000-00-00 00:00:00
[sale_price] => 0
[price] => 520000
[id] => 999039
[code] => b125nh
[shopid] => 5
)
)
)
This should do the trick.
function get_seperated_shops($all_items)
{
$seperated_shops = array();
foreach($all_items AS $item => $properties)
{
$seperated_shops[$properties['shopid']][$item] = $properties;
}
return $seperated_shops;
}
echo "<pre>";
print_r(get_seperated_shops($_SESSION['customer']['basket']));
echo "</pre>";

Categories