0

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

}
0

5 Answers 5

1

$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
       ];
    }
Sign up to request clarification or add additional context in comments.

1 Comment

What's the difference if there's a key or not tho?
0

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

4 Comments

@noob_girl Happy to help, and welcome to Stack Overflow. If this answer or any other one solved your issue, To mark an answer as accepted, click on the check mark beside the answer to toggle it from greyed out to filled in..
@noob_girl Difference is if we don't write any index like I wrote $k in my code, it will assign default keys of array which starts with 0 to increasing length of array, and last index of any array will be length of array -1. Instead of all that, I took keys which are already created in first explode.
so the output will still be the same?
Yes but for your scenario
0

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

Comments

0

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"
}]

Comments

0

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

Comments

Your Answer

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.