Vince
Vince

Reputation: 1133

create array with key and value from string

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?

Upvotes: 0

Views: 230

Answers (7)

Ahmed Ziani
Ahmed Ziani

Reputation: 1226

Try this:

$array = explode(',',$content);
$size = count($array);
for($i=0; $i<$size; $i++)
    $result[$array[$i]] = $array[++$i];

Upvotes: 4

Milen Georgiev
Milen Georgiev

Reputation: 512

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);

Upvotes: 2

Jakir Hossain
Jakir Hossain

Reputation: 2517

$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];

Upvotes: 0

bravesomesh
bravesomesh

Reputation: 33

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);

Upvotes: 0

RaMeSh
RaMeSh

Reputation: 3424

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.

Upvotes: 0

Manish Shukla
Manish Shukla

Reputation: 1365

$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);

Upvotes: 0

Sunil Pachlangia
Sunil Pachlangia

Reputation: 2061

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);

Upvotes: 0

Related Questions