Parse JSON with PHP - irregular format [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 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.

Related

Parse Nonstandard API Response into an Array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a query that gets data from an API, this is an example of what it returns.
testuser=bandwidth=36.4/128000&domain=test.com&email=test#user.com&inodes=1566
testuser=bandwidth=36.4/128000&domain=test.com&email=test#user.com&inodes=1566
testuser=bandwidth=36.4/128000&domain=test.com&email=test#user.com&inodes=1566
I want to convert this into some form of a usable array, pass it into a view as a table.
What is confusing me more is that the user name at the start is just the username, no key, just the value of username. Not sure how to convert this to an array and then loop through to output it.
This is the first two lines of raw output from the API return.
user1=bandwidth=7.05 / 128000&creator=mtemtfqx&date_created=1582530762&default=domain1.com&email_daily_limit=1000&email_deliveries_outgoing=0&inode=1492 / unlimited&ip=139.99.69.103&ips=139.99.69.103
&list=domain1.com
&package=shared5"a=32.2 / 5120&suspended=No&type=user&vdomains=1 / 5 user2=bandwidth=1.46 / 128000&creator=mtemtfqx&date_created=1583765836&default=domain2.net&email_daily_limit=1000&email_deliveries_outgoing=1&inode=2355 / unlimited&ip=139.99.69.103&ips=139.99.69.103
&list=domain2.net
This splits the input string by an =, but only 2 parts, the first part is the user, the second part are the values. Then using parse_str() it decodes the values.
These are put into an array with the user name as the key...
$output = [];
$data = 'testuser=bandwidth=36.4/128000&domain=test.com&email=test#user.com&inodes=1566
testuser1=bandwidth=36.41/128000&domain=test.com&email=test#user.com&inodes=1566
testuser2=bandwidth=36.42/128000&domain=test.com&email=test#user.com&inodes=1566';
foreach ( explode(PHP_EOL, $data ) as $line ) {
$lineData = explode("=", $line, 2);
if ( isset($lineData[1]) ) {
parse_str($lineData[1], $userData );
$output [ $lineData[0] ] = $userData;
}
}
print_r($output);
gives the output of...
Array
(
[testuser] => Array
(
[bandwidth] => 36.4/128000
[domain] => test.com
[email] => test#user.com
[inodes] => 1566
)
[testuser1] => Array
(
[bandwidth] => 36.41/128000
[domain] => test.com
[email] => test#user.com
[inodes] => 1566
)
[testuser2] => Array
(
[bandwidth] => 36.42/128000
[domain] => test.com
[email] => test#user.com
[inodes] => 1566
)
)

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)

PHP splitting a URL, delimitted by a '=' [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
Attempting to split the values after &file= for the following example URL. So i'm trying to extract the number at the end. Any suggestions?
http://blablablabla.com/test.php?type=mp4&tv=&file=3797
you can do it with many ways like using parse_url()
$query = parse_url('http://blablablabla.com/test.php?type=mp4&tv=&file=3797', PHP_URL_QUERY);
parse_str($query, $values);
print_r($values);
output will be
(
[type] => mp4
[tv] =>
[file] => 3797
)
you can also do it with using regex like below
$str="http://blablablabla.com/test.php?type=mp4&tv=&file=3797";
preg_match_all('/([^?&=#]+)=([^&#]*)/',$str,$matched);
print_r($matched);
output will be
(
[0] => Array
(
[0] => type=mp4
[1] => tv=
[2] => file=3797
)
[1] => Array
(
[0] => type
[1] => tv
[2] => file
)
[2] => Array
(
[0] => mp4
[1] =>
[2] => 3797
)
)
You can do this along with the sanitize.
$file=filter_input(INPUT_GET, "file", FILTER_SANITIZE_STRING) // This will get the data and clean the data too.
If you don't need the sanitize, you can simply use it as,
$file=$_GET['file']
I personally recommend the first method.

How to set array data posted as dropdown list in key value 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 8 years ago.
Improve this question
I want to set following array in key-value format for perticular selected dropdown value. For ex. i have array in following format
Array
(
[0] => Array
(
[text] => --Select--
[value] =>
)
[1] => Array
(
[value] => 269
[text] => Being amount paid to supplier
)
[2] => Array
(
[value] => 268
[text] => Cash Received
)
[3] => Array
(
[value] => 267
[text] => Cheque Received
)
)
Now, i want to set dropdown key-value pair as given below if i found value 268 is selected
Array
(
[0] => Array
(
[DefaultNarration_071] => 268
)
)
Can anybody please suggest me appropriate solution for above question ??
$array_list=array(array("value"=>0,"text"=>"--Select--"),array("value"=>268,"text"=>"Cash received"));
$selected=268;
$text="";
foreach($array_list as $entry){
if($entry['value']==$selected){
$text=$entry['text'];
break;
}
}
echo "Selected Option: ".$text;
Text will now contain the selected option, if I understood your question correct.

PHP Sessions and Form input names getting confused [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 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;

Categories