Suganya Rajasekar
Suganya Rajasekar

Reputation: 684

How to make a string into an array in php

I'm having a string like this..

food_item => 'Butter Croissant',food_id => '1',quantity => '1',price => '140'

I tried explode option

But I'm getting an array like this

Array
(
    [0] => 'food_item' => 'Butter Croissant'
    [1] => 'food_id' => '1'
    [2] => 'quantity' => '1'
    [3] => 'price' => '140'
)

But I should need to make it as an array like as follows,

Array
(
    [food_item] =>  'Butter Croissant'
    [food_id]   =>  '1'
    [quantity]  =>  '1'
    [price]     =>  '140'
)

how should I do this,Someone please help me..

Thank you in advance..

Upvotes: 3

Views: 151

Answers (5)

George G
George G

Reputation: 7705

Since we got answers I wanted to post different solution (given string is much like parameters string that can be parsed in array or in vars by parse_str function):

$string = "food_item => 'Butter Croissant',food_id => '1',quantity => '1',price => '140'";

$paramString = str_replace([',', " => ", "'"], ['&', '=', ''], $string);

parse_str($paramString, $data);

Now we got data as needed, and code looks a bit cleaner

Upvotes: 1

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72269

You can do it like below:-

<?php
$string = "food_item => 'Butter Croissant',food_id => '1',quantity => '1',price => '140'"; // original string

$first_array = explode(',',$string); // your first explode with `,`

$final_array = array(); // create a new empty array

foreach($first_array as $arr){ // iterate over previously exploded array
    $data = explode('=>',$arr); // now explode the value again with `=>`
    $final_array[trim($data[0])] = trim($data[1]);  // add it to final array in the form of key=>value pair
}

echo "<pre/>";print_r($final_array);

Output:-https://eval.in/609356

Note:- this is easiest stuff to do and to understand also.thanks

Upvotes: 2

Poiz
Poiz

Reputation: 7617

You could use preg_split in combination with foreach Loop for that like so:

    <?php
        $strArray       = "food_item => 'Butter Croissant',food_id => '1',quantity => '1',price => '140'";
        $arrKVPairs     = preg_split("#,\s?#", $strArray);
        $arrStrToArray  = array();

        foreach($arrKVPairs as $ikey=>$strData){
            $arrKeyVal      = preg_split("#\s?\=>\s?#", trim($strData, "'"));
            if(count($arrKeyVal) == 2){
                list($key, $val)        = $arrKeyVal;
                $arrStrToArray[$key]    = trim($val, "'");
            }
        }
        var_dump($arrStrToArray);
        // PRODUCES::

        array (size=4)
          'food_item' => string 'Butter Croissant' (length=16)
          'food_id' => string '1' (length=1)
          'quantity' => string '1' (length=1)
          'price' => string '140' (length=3)

Upvotes: 1

Jayesh Chitroda
Jayesh Chitroda

Reputation: 5039

Try:

$str = "food_item => 'Butter Croissant',food_id => '1',quantity => '1',price => '140'";
$mainArray = explode(",",$str);
$newArray = array();
foreach($mainArray as $main) {
  $innerArray = explode("=>",$main);
  $newArray[trim($innerArray[0])] = trim($innerArray[1]);
}

Output:

Array
(
    [food_item] => 'Butter Croissant'
    [food_id] => '1'
    [quantity] => '1'
    [price] => '140'
)

Upvotes: 4

vaso123
vaso123

Reputation: 12391

You can use this:

$str = "food_item => 'Butter Croissant',food_id => '1',quantity => '1',price => '140'";
$exploded = array_map('myExplode', explode(',', $str));
$result = [];
foreach ($exploded as $itemPieces) {
    $result[trim($itemPieces[0])] = trim($itemPieces[1]);
}

function myExplode($item) {
    return explode("=>", str_replace("'", '', $item));
}

var_dump($result);

OUTPUT

array (size=4)
  'food_item' => string 'Butter Croissant' (length=16)
  'food_id' => string '1' (length=1)
  'quantity' => string '1' (length=1)
  'price' => string '140' (length=3)

Upvotes: 1

Related Questions