PHP parse_str not getting all $_GET variables - php

I have a strange issue.
Would like to fetch the $_GET variables from a string with parse_str.
Which works except for the first variable.
Makes me wonder if I have written the the syntax correctly. But yes I think I (probably) did.
When I run the following URL :
system/filter/index?filter[date_selecttype]=date&filter[period_start]=08-08-2019&filter[period_end]=31-08-2019&filter[set_date]=30-08-2019&filter[booking_select_type]=booked&filter[booking_panel][6]=on&filter[booking_panel][9]=on&filter[booking_panel][1]=on&filter[booking_panel][2]=on&filter[booking_panel][11]=on&filter[booking_panel][4]=on&filter[booking_panel][5]=on&filter[booking_panel][10]=on&filter[booking_panel][7]=on&filter[booking_panel][3]=on&filter[booking_panel][12]=on&filter[booking_panel][8]=on&filter[booking_state][1]=on&filter[booking_state][0]=on&filter[booking_state][2]=on&filter[booking_state][3]=on&filter[payment_state]=&filter[select_paymentmethod]=&filter[booking_source]=&filter[booking_saleschannel]=&filter[booking_travelcode]=&filter[booking_discountcode]=&execute_filter=1
Through the following code :
$UrlStr = urldecode(str_replace('&', '&', __FULLURL__));
parse_str($UrlStr, $GETVariables);
I receive the following variables :
Array
(
[system/filter/index?filter] => Array
(
[date_selecttype] => date
)
[filter] => Array
(
[period_start] => 08-08-2019
[period_end] => 31-08-2019
[set_date] => 30-08-2019
[booking_select_type] => booked
[booking_panel] => Array
(
[6] => on
[9] => on
[1] => on
[2] => on
[11] => on
[4] => on
[5] => on
[10] => on
[7] => on
[3] => on
[12] => on
[8] => on
)
[booking_state] => Array
(
[1] => on
[0] => on
[2] => on
[3] => on
)
[payment_state] =>
[select_paymentmethod] =>
[booking_source] =>
[booking_saleschannel] =>
[booking_travelcode] =>
[booking_discountcode] =>
)
[execute_filter] => 1
)
It makes me suggest that my syntax is incorrect.
system/filter/index?filter
But then is my question. How to format it then? I thought this should work.
Whenever I set : index.php?filter it makes no difference in results.
UPDATE
Added the solution as posted by BA_Webimax
$UrlStr = urldecode( str_replace( '&', '&', __FULLURL__ ) );
$query_string = parse_url($UrlStr, PHP_URL_QUERY );
parse_str($query_string, $GETVariables );
And it works as a charm!
Array
(
[filter] => Array
(
[date_selecttype] => date
[period_start] => 08-08-2019
[period_end] => 31-08-2019
[set_date] => 30-08-2019
[booking_select_type] => booked
[booking_panel] => Array
(
[6] => on
[9] => on
[1] => on
[2] => on
[11] => on
[4] => on
[5] => on
[10] => on
[7] => on
[3] => on
[12] => on
[8] => on
)
[booking_state] => Array
(
[1] => on
[0] => on
[2] => on
[3] => on
)
[payment_state] =>
[select_paymentmethod] =>
[booking_source] =>
[booking_saleschannel] =>
[booking_travelcode] =>
[booking_discountcode] =>
)
[execute_filter] => 1
)

parse_str() was meant to work on the Query String and not a full URL. In order to properly separate the URL components, I recommend that you use parse_url() first to isolate the query string.
$UrlStr = urldecode( str_replace( '&', '&', __FULLURL__ ) );
$query_string = parse_url( $UrlStr, PHP_URL_QUERY );
parse_str( $query_string, $GETVariables );

Related

How to write Laravel Query in two parts

I am working with Laravel 5.2. I want to write a query in two parts like this:
$getData = DB::table($table)
->where($where);
$getData->first();
return $getData;
But it is not working for me. It Not provides correct data to me.
It gives:
Array ( [aggregate] => [columns] => [distinct] => [from] => countries [joins] => [wheres] => Array ( [0] => Array ( [type] => Nested [query] => Array ( [aggregate] => [columns] => [distinct] => [from] => countries [joins] => [wheres] => Array ( [0] => Array ( [type] => Basic [column] => country_name [operator] => = [value] => India [boolean] => and ) ) [groups] => [havings] => [orders] => [limit] => [offset] => [unions] => [unionLimit] => [unionOffset] => [unionOrders] => [lock] => ) [boolean] => and ) ) [groups] => [havings] => [orders] => [limit] => 1 [offset] => [unions] => [unionLimit] => [unionOffset] => [unionOrders] => [lock] => )
But it works correctly when i call like this:
$getData = DB::table($table)
->where($where)->first();
return $getData;
Can we not call a query in two parts in laravel.
You have to get back the returned data from $getData->first();
$getData = DB::table($table)
->where($where);
$getData = $getData->first(); // <----
return $getData;

change num key to string key [PHP]

I have this array :
Array ( [0] => aanneville [1] => cjaouen [2] => ebaltarejo [3] => izaalberg [4] => jfmallot [5] => matcheanauto [6] => matchfullauto [7] => pcouturier [8] => relisabeth [9] => scataldi [10] => tcouturier [11] => tlansiaux [12] => valves )
I want this :
Array ( 'login' => aanneville => cjaouen 'login' => ebaltarejo) //etc.
Because this code won't work, and I don't undersand why ??
<?php
$tab = array("valeur 1", "valeur 2", "valeur 3");
//print_r($tab);
?>
<script>
var tab= <?php echo $tab; ?> ;
console.log(tab);
</script>
I do not exactly understand your problem ,if you want to make a table of login, you can proceed as well :
$login['login']=array('anneville', 'cjaouen', 'ebaltarejo','etc..' ) ;
or
$login=array( 'login' =>array (
'log1' => 'anneville',
'log2' => 'cjaouen',
'log3' => 'ebaltarejo',
'logx' => 'etc..'
)
);
In php, array keys must be unique.But if you want to change num keys to string keys, you can use array_combine()

How to prevent declaring an associative key while hardcoding array data?

Not sure if I can word this properly but I need to create and array with some array of other information. Right now it spits out like this:
And my code goes like this:
$calNames = array(
'affordability',
'balloon-payment',
'budget',
'early-payoff',
'fuel-savings',
'lease-vs-finance',
'monthly-payment' => array( 'name' => 'test3' ),
'refinance',
'single-pay-lease',
);
print_r( $calNames );
I want it to create the array like this:
Array
(
[0] => affordability
[1] => monthly-payment
[2] => budget
[3] => early-payoff
[4] => fuel-savings
[5] => lease-vs-finance
[6] => monthly-payment => Array
(
[name] => test3
)
[7] => refinance
[8] => single-pay-lease
)
Strange way to do it, but this matches your result as close as possible:
$calNames = array(
'affordability',
'balloon-payment',
'budget',
'early-payoff',
'fuel-savings',
'lease-vs-finance',
array('monthly-payment' => array( 'name' => 'test3' )),
'refinance',
'single-pay-lease',
);
Array
(
[0] => affordability
[1] => balloon-payment
[2] => budget
[3] => early-payoff
[4] => fuel-savings
[5] => lease-vs-finance
[6] => Array
(
[monthly-payment] => Array
(
[name] => test3
)
)
[7] => refinance
[8] => single-pay-lease
)

php - shortcut for creating dynamic array with meaningful key names

I have some logic that builds a multi-dimensional array based on matches found in a regex. I call the explode function,using a delimiter.
Everything works and my array looks like this:
Array (
[0] =>
Array (
[0] => A1
[1] => 100/1000T
[2] => No
[3] => Yes
[4] => Down
[5] => 1000FDx
[6] => Auto
[7] => off
[8] => 0
)
[1] => Array (
[0] => A2
[1] => 100/1000T
[2] => No
[3] => Yes
[4] => Down
[5] => 1000FDx
[6] => Auto
[7] => off
[8] => 0
) etc.etc...
In order to keep the code in the front end "dumb", i want to change the keys from numbers to strings that represent what the values are. These strings will be used as column headings in a table. So for example:
Array (
[0] =>
Array (
[port] => A1
[Type] => 100/1000T
[Alert] => No
[Enabled] => Yes
[Status] => Down
[Mode] => 1000FDx
[MDIMode] => Auto
[FlowCtrl] => off
[BcastLimit] => 0
)
[1] => Array (
[port] => A2
[Type] => 100/1000T
[Alert] => No
[Enabled] => Yes
[Status] => Down
[Mode] => 1000FDx
[MDIMode] => Auto
[FlowCtrl] => off
[BcastLimit] => 0
) etc.etc...
Here's the code that generates this array:
$portdetailsArray = array();
foreach ($data as $portdetails) {
$pattern = '/(\s+)([0-9a-z]*)(\s+)(100\/1000T|10|\s+)(\s*)(\|)(\s+)(\w+)(\s+)(\w+)(\s+)(\w+)(\s+)(1000FDx|\s+)(\s*)(\w+)(\s*)(\w+|\s+)(\s*)(0)/i';
if (preg_match($pattern, $portdetails, $matches)) {
$replacement = '$2~$4~$8~$10~$12~$14~$16~$18~$20';
$portdetails= preg_replace($pattern, $replacement, $portdetails);
array_push($portdetailsArray, explode('~',$portdetails));
}
}
I guess instead of using the explode function, I can manually loop through my string. Each time I find a "~", i know it's the start of a new field so i can add they key /value pair manually.
But I was just wondering if anyone had ideas on other ways to do this.
Thanks.
To reply to your original question, you could use the array_combine function, to replace the keys.
$row = explode('~',$portdetails);
$row = array_combine(array(
'port',
'Type',
'Alert',
'Enabled',
'Status',
'Mode',
'MDIMode',
'FlowCtrl',
'BcastLimit'), $row);
But even better, you should use the clearer (verbose is clearer in this case)
if (preg_match($pattern, $portdetails, $matches)) {
array_push($portdetailsArray, array(
'port' => $matches[2],
'Type' => $matches[4],
'Alert' => $matches[8],
'Enabled' => $matches[10],
'Status' => $matches[12],
'Mode' => $matches[14],
'MDIMode' => $matches[16],
'FlowCtrl' => $matches[18],
'BcastLimit' => $matches[20]));
}

how can i access such a php array

i was trying to access this php array with no luck, i want to access the [icon] => icon.png
Array ( [total] => 2
[total_grouped] => 2
[notifys] => Array ( [0] => Array ( [notifytype_id] => 12
[grouped] => 930
[icon] => icon.png
[n_url] => wall_action.php?id=930
[desc] => 690706096
[text] => Array ( [0] => Sarah O'conner ) [total] => 1 )))
$arr['notifys'][0]['icon']
ETA: I'm not sure what your comment means, the following code:
$arr = array('total'=>2, 'total_grouped' => 2, 'notifys' => array(array(
'notifytype_id' => 12, 'icon' => 'icon.png')));
echo '<pre>';
print_r($arr);
echo '</pre>';
var_dump($arr['notifys'][0]['icon']);
outputs:
Array
(
[total] => 2
[total_grouped] => 2
[notifys] => Array
(
[0] => Array
(
[notifytype_id] => 12
[icon] => icon.png
)
)
)
string(8) "icon.png"
Generally, code never outputs nothing. You should be developing with all errors and notifications on.
$arr['notifys'][0]['icon']
rg = Array ( [total] => 2 [total_grouped] => 2 [notifys] => Array ( [0] => Array ( [notifytype_id] => 12 [grouped] => 930 [icon] => icon.png [n_url] => wall_action.php?id=930 [desc] => 690706096 [text] => Array ( [0] => Sarah O'conner ) [total] => 1 )));
icon = rg["notifsys"][0]["icon"];
Everybody is posting right answer. Its just you have giving a wrong deceleration of array.
Try var_dump/print_r of array and then you can easily understand nodes.
$arr = array(total => 2,
total_grouped => 2,
notifys => array( 0 => array(notifytype_id => 12,
grouped => 930,
icon => 'icon.png',
n_url => 'wall_action.php?id=930',
desc => 690706096,
text =>array(0 => 'Sarah Oconner' ),
total => 1,
),
),
);
echo $arr['notifys']['0']['icon'];

Categories