0

I am trying to create an array but have no luck.

When I dump my array I get from results, looks ok. I need to create a new array for next processing.

array(1) { [0]=> array(3) { 
          ["name"]=> string(37) "America , United States , City" 
          ["link"]=> string(48) "https://www.test.com" 
          ["icon"]=> string(50) "  " 
                          } 
         }

Based on the array above I need to create array

[0]
  ["name"]=>"America",
  [link]=>"https://www.test.com",
  ["icon"]=> string(50) "  "
[1]
  ["name"]=>"United States",
  [link]=>"https://www.test.com",
  ["icon"]=> string(50) "  "
[2]
  ["name"]=>"City",
  [link]=>"https://www.test.com",
  ["icon"]=> string(50) "  "

Basically, after the separator is "," and it defines how many indexes array will have. In this case, we have three, but for example, we can have only ["name"]=>"America" and it will have only 1 index.

Appreciate any kind of help.

1
  • Please share what output/result you needed? Commented Jan 29, 2020 at 8:04

1 Answer 1

1

You can explode first index and then do a loop for creating resultant array:

$names = explode(",",$data[0]['name']);    // <-- all names in an array

$res = [];
foreach($names as $name){
    $tmp = [
        "name" => trim($name),
        "link" => $data[0]['link'],
        "icon" => $data[0]['icon'],       
    ];
    $res[] = $tmp;
}

print_r($res);

Demo

Sign up to request clarification or add additional context in comments.

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.