Asked  7 Months ago    Answers:  5   Viewed   55 times

How can I turn a string below into an array?

pg_id=2&parent_id=2&document&video 

This is the array I am looking for,

array(
    'pg_id' => 2,
    'parent_id' => 2,
    'document' => ,
    'video' =>
)

 Answers

98

You want the parse_str function, and you need to set the second parameter to have the data put in an array instead of into individual variables.

$get_string = "pg_id=2&parent_id=2&document&video";

parse_str($get_string, $get_array);

print_r($get_array);
Tuesday, June 1, 2021
 
FWH
answered 7 Months ago
FWH
77

Use PHP's explode.

$str = "1,2,3,4,5,6";
$arr = explode("," $str); // array( '1', '2', '3', '4', '5', '6' );

foreach ($arr AS $index => $value)
    $arr[$index] = (int)$value; 

// casts each value to integer type -- array( 1, 2, 3, 4, 5, 6 );

As suggested by Tim Cooper, using array_walk is simpler than the above loop:

array_walk($arr, 'intval');
Wednesday, March 31, 2021
 
Magnanimity
answered 9 Months ago
94

To add to Rikesh's answer:

<?php
$aryMain = array(array('hello','bye'), array('',''),array('','')); 
$aryMain = array_filter(array_map('array_filter', $aryMain));
print_r($aryMain);

?>

Sticking his code into another array_filter will get rid of the entire arrays themselves.

Array
(
    [0] => Array
        (
            [0] => hello
            [1] => bye
        )

)

Compared to:

$aryMain = array_map('array_filter', $aryMain);

Array
(
    [0] => Array
        (
            [0] => hello
            [1] => bye
        )

    [1] => Array
        (
        )

    [2] => Array
        (
        )

)
Wednesday, March 31, 2021
 
drowneath
answered 9 Months ago
10

From the example, it looks to me like you are hoping to get this:

var rows = XDocument.Load("filename.xml")
                    .Root.Elements()
                    .Select(row => row.Elements()
                                      .ToDictionary(v => v.Attribute("name").Value,
                                                    v => v.Attribute("value").Value);

This will create an IEnumerable<Dictionary<string, string>>, where each entry in the IEnumerable is a "row" and each entry in the inner dictionaries is a "variable".


If you are looking to create a more strongly-typed solution, and can guarantee the XML structure is always two variables, one named budgetYear and one named account, then something like this would work:

// Probably needs a better name.
struct YearAndAccount
{
    private readonly int budgetYear;
    private readonly string accountId;

    public int BudgetYear { get { return this.budgetYear; } }
    public string AccountId { get { return this.accountId; } }

    public YearAndAccount(int budgetYear, string accountId)
    {
        this.budgetYear = budgetYear;
        this.accountId = accountId;
    }
}

var rows = XDocument.Load("filename.xml")
                    .Root.Elements()
                    .Select(row => new YearAndAccount(
                        int.Parse(row.Elements().Single(el => el.Attribute("name").Value == "budgetYear")
                                                      .Attribute("value").Value),
                        row.Elements().Single(el => el.Attribute("name").Value == "account")
                                      .Attribute("value").Value));

This will create an IEnumerable<YearAndAccount>.


The parsing code for the strongly-typed solution is so amazingly icky because your XML is very poorly structured; a better structure would be something like

<details>
 <yearAndAccount>
   <budgetYear>2008</budgetYear>
   <account>10202</account>
 </yearAndAccount>
 <yearAndAccount>
   <budgetYear>2007</budgetYear>
   <account>11202</account>
 </yearAndAccount>
</details>

in which case the code would simply be

var rows = XDocument.Load("filename.xml")
                    .Root.Elements()
                    .Select(row => new YearAndAccount(row.Element("budgetYear").Value,
                                                      int.Parse(row.Element("account").Value)));
Tuesday, October 26, 2021
 
MGP
answered 1 Month ago
MGP
53

The Google Toolbox for Mac contains a GTMNSDictionaryURLArgumentsAdditions category on NSDictionary which may do what you want.

If you have control over the query string (on the client side), you could send an encoded plist which can be decoded directly into an NSDictionary.

Wednesday, November 24, 2021
 
Klausos Klausos
answered 1 Week ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share