change num key to string key [PHP] - 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()

Related

PHP parse_str not getting all $_GET variables

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

PHP - Converting Array to group duplicates together - phpspreadsheet

I have a csv uploader i'm creating to push to an order api. Using phpSpreadsheets toArray method, i have an array like so:
Array
(
[0] => Array
(
[0] => product_sku
[1] => product_qty
[2] => shipping_name
[3] => shipping_address1
[4] => shipping_address2
[5] => shipping_city
[6] => shipping_county
[7] => shipping_postcode
[8] => shipping_type
[9] => customer_id
)
[1] => Array
(
[0] => test_sku_1
[1] => 3
[2] => Bruce Wayne
[3] => The Manor
[4] => Near Arkahm Asylumn
[5] => Gotham
[6] => Greater Gothan
[7] => B17MAN
[8] => 1
[9] => 14994333
)
[2] => Array
(
[0] => test_sku_2
[1] => 2
[2] => Bruce Wayne
[3] => The Manor
[4] => Near Arkahm Asylumn
[5] => Gotham
[6] => Greater Gothan
[7] => B17MAN
[8] => 1
[9] => 14994333
)
[3] => Array
(
[0] => test_sku_3
[1] => 7
[2] => Bruce Wayne
[3] => The Manor
[4] => Near Arkahm Asylumn
[5] => Gotham
[6] => Greater Gothan
[7] => L17MA2
[8] => 1
[9] => 14994333
)
)
Each order will be on a new line, however if a customer orders two different items, i need to group them together using their postcode. As such i was aiming to rearange into this format:
[orders] => Array(
Array(
[shipping_name] => "Bruce wayne",
[customer_id] => 14994333,
[address] => Array(
[shipping_address1] => "The Manor",
[shipping_address2] => "Near Arham Asylumn",
[shipping_city] => "Gotham",
[shipping_county] => "Greater Gotham",
[shipping_postcode] => "B17MAN",
)
[products] => Array(
Array(
[sku] => "test_sku_1",
[quantity] => 3
),
Array(
[sku] => "test_sku_2",
[quantity] => "2"
)
)
)
)
Once of the first problems i encountered was trying to match the postcodes. I managed to get a count using:
$getDuplicates = array_count_values(array_map(function($duplicates) {
return $duplicates[7]; //Where 7 is the postcode
}, $rows));
This worked in counting what i needed correctly. However from there i'm hitting a brick wall. If i'm counting the duplicates, i need it to also make a note of the rows it's already gone through so they aren't pushed incorrectly into the new array i want to make.
Pseudo it should be:
for each rows as row{
if row isn't set to be ignored{
for each count of this postcode{
array_push the product sku and qty
mark these rows as rows to be ignored
}
}
}
Can anyone help me with this?
So, long story short, i've been so caught up in this i completely missed the obvious. To solve this, i took the original array from phpspreadsheet and did an array_multisort:
foreach ($rows as $key => $row) {
$postcodes[$key] = $row[7]; //the postcode key
}
array_multisort($postcodes, SORT_DESC, $rows;
I then just worked my way through the array and if the postcode changed, i'd create a new array, otherwise i'd just push straight into the correct array for the products.
I feel really stupid i didn't think of this before. Thank you to #apokryfos for trying to help!

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
)

find non duplicated item in an array

I have two arrays built from different directories that contain file names stripped of extensions. I want to find the ones that don't make a pair thus I merged the array to obtain the array below. How can I find the only non duplicate item in an array?
Array
(
[0] => dbbackup_2014.09.03_07_06_27
[1] => dbbackup_2014.09.03_07_07_08
[2] => dbbackup_2014.09.03_07_13_33
[3] => dbbackup_2014.09.03_07_15_24
[4] => dbbackup_2014.09.03_07_21_57
[5] => dbbackup_2014.09.03_07_22_11
[6] => dbbackup_2014.09.03_08_40_35
[7] => dbbackup_2014.09.03_08_41_36
[8] => dbbackup_2014.09.03_08_43_38
[9] => dbbackup_2014.09.04_04_59_08
[10] => dbbackup_2014.09.03_07_06_27
[11] => dbbackup_2014.09.03_07_07_08
[12] => dbbackup_2014.09.03_07_13_33
[13] => dbbackup_2014.09.03_07_15_24
[14] => dbbackup_2014.09.03_07_21_57
[15] => dbbackup_2014.09.03_07_22_11
[16] => dbbackup_2014.09.03_08_40_35
[17] => dbbackup_2014.09.03_08_41_36
[18] => dbbackup_2014.09.03_08_43_38
)
Note: it is [9]
$a = array_flip(array_filter(array_count_values($a),function($item){
return $item == 1 ? true : false;
}));
print_r($a);
Output
Array
(
[1] => dbbackup_2014.09.04_04_59_08
)
Ideone
foreach($array as $data)
{
$values=explode("_",$data);
$output[$values[1]]++;
}
foreach($output as $date=>$number)
{
if($number==1)
echo $date;
}
Output:
2014.09.04
Fiddle

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]));
}

Categories