John Davidson

how to create json files from 3 php arrays

0 comments
Message:


I have 3 arrays in php and i want to create for each value a json file:


$aa = array('jack', 'joe', 'john');
$bb = array('audi', 'bmw', 'mercedes');
$cc = array('red', 'blue', 'gray');

foreach($aa as $a) {
$data['name'] = $a;
foreach($bb as $b) {
$data['car'] = $b;
}
foreach($cc as $c) {
$data['color'] = $c;
}

$data_file = 'data/'.$a.'.json'; // jack.json and joe.json and john.json
$json_data = json_encode($data, JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT);
file_put_contents($data_file,$json_data);

}

My json files should look like this:


jack.json


{
"name": "jack",
"car": "audi",
"color": "red"
}

joe.json


{
"name": "joe",
"car": "bmw",
"color": "blue"
}

john.json


{
"name": "john",
"car": "mercedes",
"color": "gray"
}

I do not succeed in it with the code above: fields car and color stay empty in each json file...



My Answer:
"name": "jack",_ "car": "audi",_ "color": "red"_}_joe.json_{_ "name": "joe",_ "car": "bmw",_ "color": "blue"_}_john.json_{_ "name": "john",_ "car": "mercedes",_ "color": "gray"_}_This code will create three JSON files, one for each value in the $aa array. Each JSON file will contain the corresponding values from the $bb and $cc arrays.

Rate this post

4 of 5 based on 9117 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us