Parse Nonstandard API Response into an Array [closed] - php

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
)
)

Related

How to limit sub array count to 5 for all id values in multi dimensional array php? [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 multi dimensional output array with following structure :
Array
(
[0] => Array
(
[id] => 5925
[fb_id] => 123
[description] =>
[video] =>
[thum] =>
)
[1] => Array
(
[id] => 7060
[fb_id] => 2344
[description] =>
[video] =>
[thum] =>
)
[2] => Array
(
[id] => 6579
[fb_id] => 123
[description] =>
[video] =>
)
)
Here, I want to limit the sub array count to 5 for all users with same 'fb_id'. Only 5 sub arrays for each fb_id should be in final output array. Any help would be appreciable.
The original array being used here is called $original and this code will reduce it down and produce a new array called $new. Feel free to change the variable names as appropriate.
$new = [];
$count = [];
foreach ($original as $element) {
$fbid = $element['fb_id'];
if (!isset($count[$fbid])) {
$count[$fbid] = 0;
}
if ($count[$fbid] < 5) {
$new[] = $element;
$count[$fbid]++;
}
}
// New array with limited sub-arrays
print_r($new);

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.

Rearrange php Array structure [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
Is there any simple solution to convert php mysql result change array structure with foreach or any method ?
Current result
Array
(
[1033] => stdClass Object
(
[id] => 1033
[plugin] => kooyke
[name] => kooyduration
[value] => 13
)
[1029] => stdClass Object
(
[id] => 1029
[plugin] => kooyke
[name] => kooyendpoint
[value] => http://localhost/public/data/
)
[1030] => stdClass Object
(
[id] => 1030
[plugin] => kooyke
[name] => kooylogin
[value] => ryrtrtr68fds876fdsf876fsd87fd
)
)
Expecting result
Array
(
[kooyduration] => 13
[kooyendpoint] => http://localhost/public/data/
[kooylogin] => ryrtrtr68fds876fdsf876fsd87fd
)
the code trying to convert
foreach($result as $value){
$expresult[] = $value['value'];
}
print_r($expresult);
Bellow code blocks may help you.
$i = 0;
$last_arr = array();
$new_arr = array();
foreach($my_array as $key=>$val)
{
$new_arr[$val->name] = $val->value;
}
array_push($last_arr,$new_arr);
You can not use like $value['value'], should be $value->value because of object. And push new array your name index. Try following:
$expresult = array();
foreach($result as $value)
{
$expresult[$value->name] = $value->value;
}

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.

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