0

I'm working on a private project and i have the following issue.

This is what i have:

array(3) {
  [0]=>
  array(2) {
    ["import_key"]=>
    string(2) "id"
    ["import_value"]=>
    string(6) "345412"
  }
  [1]=>
  array(2) {
    ["import_key"]=>
    string(4) "date"
    ["import_value"]=>
    string(10) "21-08-2017"
  }
  [2]=>
  array(2) {
    ["import_key"]=>
    string(4) "name"
    ["import_value"]=>
    string(53) "Psy-Fi shuttle bus Psy-Fi Festival - Schiphol Airport"
 }
}

What i want is, that the import_key will be assign as key and the import_value as value.

How can i do this.

Thnks a lot!

1 Answers1

2

EDIT: Updated after your comments

So after your comments below here is the updated code:

$cleanArray = array();
$resultArray = array();
foreach ($array as $sub) {
    $cleanArray[$sub["import_key"]] = $sub["import_value"];
}
$resultArray = array($cleanArray['id']=>$cleanArray);

What this now does, is using the same original code before, it then adds the $cleanArray array to a new array, $resultArray, which takes the key from the 'id' value of $cleanArray.

Please note, this will fail if you end up having multiple sets of data in your original $array, with two or more id's etc. To deal with that, you would need to do an extra foreach loop and a bit more logic.

Original Response:

If I understand your question correctly you want to iterate through the array to get something like:

Array (
    [id] => 345412
    [date] => 21-08-2017
    [name] => Psy-Fi shuttle bus Psy-Fi Festival - Schiphol Airport
)

To do this, I set your first array to a variable, imaginatively called $array.

Then use the following function and the above output will be returned as $cleanArray:

$cleanArray = array();  
foreach ($array as $sub) {
    $cleanArray[$sub["import_key"]] = $sub["import_value"];
}

What this does is creates a blank array called $cleanArray, loops through each of the indexes in $array and then adds the values to $cleanArray, as a key / value pair.

Hope this is what you wanted.

Dom_TC
  • 83
  • 6
  • and if i want a uuid as top key, like? $cleanArray[$sub['import_uuid'] => [$sub["import_key"]] = $sub["import_value"]; it doesn't work this way right :S – Pieter Dijkstra Apr 09 '17 at 00:05
  • Sorry, I don't think I understand what you want to end up with? Could you clarify a bit. But no, that won't work as all the $sub array contains is each inside array from $array so for the first iteration, $sub contains: array ( "import_key" => "id", "import_value" => "345412" ). This means there is no 'import_uuid' for it to reference... – Dom_TC Apr 09 '17 at 10:40
  • What i want is: 123 => [id => 123, name => 123]; etc. – Pieter Dijkstra Apr 09 '17 at 10:42
  • Sorry i'm still not entirely sure what you're asking... Could you explain it using the array in your question, as your example above, make's it very hard to know if the initial key is coming from 'id' or 'name' etc. – Dom_TC Apr 09 '17 at 11:20
  • this is what i want: https://codeshare.io/5w9RA7 – Pieter Dijkstra Apr 09 '17 at 12:41
  • Cool that makes sense - let me just double check some new code and I'll get back to you. – Dom_TC Apr 09 '17 at 12:43
  • I've edited my comment with some new code. – Dom_TC Apr 09 '17 at 13:10
  • Thnx a lot, man! – Pieter Dijkstra Apr 09 '17 at 19:02