0

I have a string:

$content = "test,something,other,things,data,example";

I want to create an array where the first item is the key and the second one the value.

It should look like this:

Array
(
    [test] => something
    [other] => things
    [data] => example
)

How can I do that? It's difficult to search for a solution because I don't know how to search this.

It's very similar to this: Explode string into array with key and value

But I don't have a json array.

I tried something like that:

$content = "test,something,other,things,data,example";

$arr = explode(',', $content);

$counter = 1;
$result = array();

foreach($arr as $item) {
    if($counter % 2 == 0) {
        $result[$temp] = $item;
        unset($temp);
        $counter++;
    } else {
        $temp = $item;
        $counter++;
        continue;
    }
}

print_r($result);

But it's a dirty solution. Is there any better way?

Community
  • 1
  • 1
Vince
  • 1,133
  • 3
  • 17
  • 34

7 Answers7

4

Try this:

$array = explode(',',$content);
$size = count($array);
for($i=0; $i<$size; $i++)
    $result[$array[$i]] = $array[++$i];
Ahmed Ziani
  • 1,206
  • 3
  • 14
  • 26
2

Try this:

$content = "test,something,other,things,data,example";
$data = explode(",", $content);// Split the string into an array
$result = Array();
$size = count($data); // Calculate the size once for later use
if($size%2 == 0)// check if we have even number of items(we have pairs)
for($i = 0; $i<$size;$i=$i+2){// Use calculated size here, because value is evaluated on every iteration
$result[$data[$i]] = $data[$i+1];
}

var_dump($result);
Milen Georgiev
  • 502
  • 3
  • 13
0

Try this

$content = "test,something,other,things,data,example";
$firstArray = explode(',',$content);

print_r($firstArray);

$final = array();
for($i=0; $i<count($firstArray); $i++)
{
    if($i % 2 == 0)
    {
        $final[$firstArray[$i]] = $firstArray[$i+1];
    }   
}
print_r($final);
Sunil Pachlangia
  • 2,033
  • 2
  • 15
  • 25
0

You could able to use the following:

$key_pair = array();
$arr = explode(',', $content);
$arr_length = count($arr);
if($arr_length%2 == 0)
{
   for($i = 0; $i < $arr_length; $i = $i+2)
   {
     $key_pair[$arr[$i]] = $arr[$i+1];
   }
}
print_r($key_pair);
SaidbakR
  • 13,303
  • 20
  • 101
  • 195
0
$content = "test,something,other,things,data,example";
$x = explode(',', $content);

$z = array();
for ($i=0 ; $i<count($x); $i+=2){
    $res[$x[$i]] = $x[$i+1];
    $z=array_merge($z,$res);
}

print_r($z);
Manish Shukla
  • 1,355
  • 2
  • 8
  • 21
0

I have tried this example this is working file.

Code:-

    <?php
 $string = "test,something|other,things|data,example";
 $finalArray = array();

 $asArr = explode( '|', $string );
 foreach( $asArr as $val ){
 $tmp = explode( ',', $val );
 $finalArray[ $tmp[0] ] = $tmp[1];
 }
 echo "After Sorting".'<pre>';
 print_r( $finalArray );
 echo '</pre>';
 ?>

Output:-

Array
(
    [test] => something
    [other] => things
    [data] => example
)

For your reference check this Click Here

Hope this helps.

RaMeSh
  • 3,330
  • 2
  • 19
  • 31
0
$content = "test,something,other,things,data,example";

$contentArray = explode(',',$content);
for($i=0; $i<count($contentArray); $i++){
    $contentResult[$contentArray[$i]] = $contentArray[++$i];
}
print_r( $contentResult);

Output

Array
(
    [test] => something
    [other] => things
    [data] => example
)
  1. $contentResult[$contentArray[1]] = $contentArray[2];
  2. $contentResult[$contentArray[3]] = $contentArray[4];
  3. $contentResult[$contentArray[5]] = $contentArray[6];
Jakir Hossain
  • 2,457
  • 18
  • 23