1

I'm using PHP & JSON, i need add a key to each json array

$array1 = array('id','name1','name2','name3','name4');

I get to many data by sql-query:

$array2 = [["1","1","12","34","text1"],["2","1","56","78","text2"]] // json array

I need add a key to each json array, like:

[{"id":"1","name1":"1","name2":"1","name3":"1","name4":"test1"},{"id":"2","name1":"2","name2":"2","name3":"2","name4":"test2"}]
1
  • 3
    Use array_combine Commented May 31, 2017 at 12:10

4 Answers 4

1

Try this

The array_combine() function creates an array by using the elements from one "keys" array and one "values" array.

<?php
$array1 = array('id','name1','name2','name3','name4');

$array2 = [["1","1","12","34","text1"],["2","1","56","78","text2"]];
$result = array();
//echo "<pre>";
foreach ($array2 as $value) {
    $result[] = array_combine($array1,$value);
}
//print_r($result);
echo json_encode($result);
 ?>

NOTE: Here array elements of $array1 and array elements of $value must be same otherwise it returns FALSE.

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

Comments

1

You can use array_combine() to create an array from two other using one for keys and the other for values (Note both must have the same length!)

$array1 = array('id','name1','name2','name3','name4');
$array2 = [["1","1","12","34","text1"],["2","1","56","78","text2"]];

foreach ($array2 as &$set) {
    $set = array_combine($array1, $set);
}
unset($set);

echo json_encode($array2);

Here we pass each inner array into the foreach by reference (the & in &$set) so we can modify it directly without having to create a third result array, we also unset it after the loop.

Comments

0

You should use the definition of array(), which is:

array(
    key  => value,
    key2 => value2,
    key3 => value3,
    ...
    )

You have your data in each element of array2, you can iterate it, and create a new array for each element using the constructor that I have posted. So, an example will be:

array('id'-->array2[i][j],'name1'=>array[i][j+1],...)

Comments

0

You can use functional iteration to avoid temporary global variables.

Code: (Demo)

$keys = ['id', 'name1', 'name2', 'name3', 'name4'];

$rows = [
    ["1", "1", "12", "34", "text1"],
    ["2", "1", "56", "78", "text2"]
];

echo json_encode(
         array_map(
             function($row) use ($keys) {
                 return array_combine($keys, $row);
             },
             $rows
         )
     );

Or with PHP7.4 and higher enjoy arrow function syntax:

echo json_encode(
         array_map(
             fn($row) => array_combine($keys, $row),
             $rows
         )
     );

Ultimately, though, if this data is coming from any kind of sql source, then you should be managing the column names in the SELECT clause and then php simply creates associative subarrays as you fetch and push the data into your result array.

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.