3

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

Death-is-the-real-truth
72.3k10 gold badges58 silver badges105 bronze badges
asked Jul 21, 2016 at 11:32
5
  • Your first "string" is an array, not a string Commented Jul 21, 2016 at 11:34
  • Maybe he has it as a string... Commented Jul 21, 2016 at 11:35
  • Maybe, but it is a little confusing Commented Jul 21, 2016 at 11:36
  • If you explode the string using ',' the part of string "food_item => 'Butter Croissant' " will considered as a string Commented Jul 21, 2016 at 11:36
  • What if food name contains a ,? Commented Jul 21, 2016 at 11:41

5 Answers 5

4

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'
)
answered Jul 21, 2016 at 11:35

1 Comment

Thanks for your answer..:)
2

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

answered Jul 21, 2016 at 11:37

3 Comments

@SuganyaRajasekar glad to help you
@ Anant Yes I did it :)
Care with comma's in your string. I would advice a regex. You have a problem with $string = "food_item => 'Butter, Croissant'...
1

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)
answered Jul 21, 2016 at 11:37

Comments

1

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)
answered Jul 21, 2016 at 11:42

Comments

1

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

answered Jul 21, 2016 at 12:07

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.