1

I have string as

SportId : 56,GroundType : Public,SelectArea : 10,Cost : 3000-4000 ,Size : 7 * 7

when explode this array output is

Array
(
    [0] => SportId : 56
    [1] => GroundType : Public
    [2] => SelectArea : 10
    [3] => Cost : 3000-4000 
    [4] => Size : 7 * 7
)

I want output in associative array as

 Array
(
    ['SportId'] => 56
    ['GroundType'] => Public
    ['SelectArea'] => 10
    ['Cost'] => 3000-4000 
    ['Size'] => 7 * 7
)
Nagesh Katke
  • 540
  • 7
  • 23

1 Answers1

4

This should do:

<?php

$info = "SportId : 56,GroundType : Public,SelectArea : 10,Cost : 3000-4000 ,Size : 7 * 7";
$arrInfo = explode(",",$info);

$newArray = [];
foreach($arrInfo as $item) {
    $values = explode(":",$item);
    $newArray[$values[0]] = $values[1];
}

print_r($newArray);
deChristo
  • 1,860
  • 2
  • 17
  • 29
  • An array is not supposed to have duplicate keys. Take a look at: http://stackoverflow.com/questions/2879132/php-associative-array-duplicate-keys – deChristo Dec 19 '16 at 13:29
  • but I need to store both values as I want to pass them into sql where condition – Nagesh Katke Dec 19 '16 at 13:34
  • If by both values you mean the key and the value you can do a: foreach($item as $key => $value) and get them. – deChristo Dec 19 '16 at 13:38