noob_girl
noob_girl

Reputation: 5

Multi-dimensional array and explode function in PHP

I'm having a hard time to get my desired output regarding multi-dimensional array please help me out thankssss

Desired output:

[
    {
        "sku" : "11111",
        "qty" : "1"
    },
    {
        "sku" : "22222",
        "qty" : "1"
    },
    {
        "sku" : "33333",
        "qty" : "1"
    },
    {
        "sku" : "44444",
        "qty" : "1"
    }
]

My code:

$string = "11111*1; 22222*1; 33333*1; 44444*1";

$array  = explode('; ', $string);

foreach($array as $k=>$v){

  $array[$k] = explode('*', $v);

}

Upvotes: 0

Views: 128

Answers (5)

Rakesh Jakhar
Rakesh Jakhar

Reputation: 6388

You can use array_walk, explode and json_encode with parameter JSON_PRETTY_PRINT

$string = "11111*1; 22222*1; 33333*1; 44444*1";
$strToArray = explode(';',$string);
$res=[];
array_walk($strToArray, function($v, $k) use (&$res){
  $res[$k] = [
    'sku' => substr($v, 0, strpos($v, '*')),
    'qty' => substr($v,strpos($v, '*')+1, strlen($v))
  ];
});
echo json_encode($res,JSON_PRETTY_PRINT);

Live Demo

Upvotes: 0

Rahul
Rahul

Reputation: 18567

You just need to fetch those values in two different variables and just assign it to new variable say $result

$string = "11111*1; 22222*1; 33333*1; 44444*1";
$array  = explode('; ', $string);
foreach ($array as $k => $v) {
    list($sku, $qty)   = explode('*', $v);
    $result[$k]['sku'] = $sku;
    $result[$k]['qty'] = $qty;
}
echo json_encode($result);

json_encode — Returns the JSON representation of a value
list — Assign variables as if they were an array

Output

[{
    "sku": "11111",
    "qty": "1"
}, {
    "sku": "22222",
    "qty": "1"
}, {
    "sku": "33333",
    "qty": "1"
}, {
    "sku": "44444",
    "qty": "1"
}]

Demo.

EDIT

You can use array_combine to achieve the same.

foreach ($array as $k => $v) {
    $result[$k]   = array_combine(['sku', "qty"],explode('*', $v));
}

Demo

Upvotes: 0

Slava
Slava

Reputation: 938

$key is not required in quickSwap answer:

    $string = "11111*1; 22222*1; 33333*1; 44444*1";
    $array  = explode('; ', $string);
    foreach ($array as $value) {
        list($sku, $qty) = explode('*', $value);
        $result[] = [
            'sku' => $sku,
            'qty' => $qty
       ];
    }

Upvotes: 1

Eddie
Eddie

Reputation: 26854

One option is to use array_map to loop thru the exploded string. Use json_encode to convert the array into a json

$string = "11111*1; 22222*1; 33333*1; 44444*1";

$result = array_map(function($v){
    $t =  explode("*", $v);
    return array( "sku" => trim( $t[0] ), "qty" => trim( $t[1] ));      
}, explode(";", $string));

echo json_encode( $result );

This will result to:

[{
  "sku": "11111",
  "qty": "1"
}, {
  "sku": "22222",
  "qty": "1"
}, {
  "sku": "33333",
  "qty": "1"
}, {
  "sku": "44444",
  "qty": "1"
}]

Upvotes: 0

M. Eriksson
M. Eriksson

Reputation: 13635

You're currently iterate through the object correctly, but you need to build your array in the correct way, containing the key names:

$string = "11111*1; 22222*1; 33333*1; 44444*1";
$array  = explode('; ', $string);
$new    = [];

foreach($array as $k => $v){
    $parts = explode('*', $v);

    $new[] = [
        'sku' => $parts[0],
        'qty' => $parts[1],
    ];
}

Here's a demo

Upvotes: 0

Related Questions