Reputation: 65
I have two different Json
files, each generated from a different php array. They both contain one piece of similar information that I want to link together in one document.
Php array one
looks like this:
$HisNameIsMyName = array(
'His' => 'John',
'Name' => 'Jacob',
'Is' => 'Jacob',
'My' => 'Jacob',
'Name2' => 'Shmidt');
And was converted into Json
via the following code:
$compiled = json_decode(file_get_contents('compiled.json'), true);
foreach ($HisNameIsMyName as $word => $publisher) {
$compiled[$word]['publisher'] = $publisher;
}
file_put_contents('compiled.json', json_encode($compiled, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
And output the following:
{
"His": {
"publisher": "John"
},
"Name": {
"publisher": "Jacob"
},
"Is": {
"publisher": "Jacob"
},
"My": {
"publisher": "Jacob"
},
"Name2": {
"publisher": "Shmidt"
}
}
Each Publisher
has been assigned a number in a different array and looks like the following:
$numberToPublisher = array(
"John" => 1,
"Jacob" => 2,
"Jingle" => 3,
"Himer" => 4,
"Shmidt" => 5
How would I take the number assigned to the Publisher
in $numberToPublisher
and add it to the Json
so that it would look like the following:
{
"His": {
"publisher": "John"
"pubNumber": "1"
},
"Name": {
"publisher": "Jacob"
"pubNumber": "2"
},
"Is": {
"publisher": "Jacob"
"pubNumber": "2"
},
"My": {
"publisher": "Jacob"
"pubNumber": "2"
},
"Name2": {
"publisher": "Shmidt"
"pubNumber": "5"
}
}
Upvotes: 1
Views: 25
Reputation: 350147
Extend your existing loop like this:
foreach ($HisNameIsMyName as $word => $publisher) {
$compiled[$word]['publisher'] = $publisher;
$compiled[$word]['pubNumber'] = $numberToPublisher[$publisher];
}
Upvotes: 1