priya priya - 3 months ago 21
JSON Question

Imagine that two arrays containing book and author information have been pulled from a database:

Display the resulting $books array .As the result.

$authors = array( “Steinbeck”, “Kafka”, “Tolkien”, “Dickens”, “Milton”,
“Orwell” );
$books = array(
Array(“title” = > “The Hobbit”,“authorId” = > 2,“pubYear” = > 1937),
Array(“title” = > “The Grapes of Wrath”,“authorId” = > 0,“pubYear” = > 1939),
Array(“title” = > “A Tale of Two Cities”,“authorId” = > 3,“pubYear” = > 1859),
Array(“title” = > “Paradise Lost”,“authorId” = > 4,“pubYear” = > 1667),
Array(“title” = > “Animal Farm”,“authorId” = > 5,“pubYear” = > 1945),
Array(“title” = > “The Trial”,“authorId” = > 1,“pubYear” = > 1925),);


Instead of containing author names as strings, the $books array contains numeric indices (keyed on “ authorId “ ) pointing to the respective elements of the $authors array. Write a script to add an “ authorName ” element to each associative array within the $books array that contains the author name string pulled from the $authors array. Display the resulting $books array in a Web page.

Answer

Whenever you ask any question on SO always remember question should be clear and must have code which you have tried expect.

Now Try this code this will add a new index authorName in $books array

Loop through $books array and check if authorId exist in $authors and assign author name to new index authorNamein $books

<?php
$authors = array("Steinbeck", "Kafka", "Tolkien", "Dickens", "Milton",
    "Orwell" );

$books = array(
array("title" =>"The Hobbit","authorId" => 2,"pubYear" => 1937),
array("title" =>"The Grapes of Wrath","authorId" => 0,"pubYear" => 1939),
array("title" =>"A Tale of Two Cities","authorId" => 3,"pubYear" => 1859),
array("title" =>"Paradise Lost","authorId" => 4,"pubYear" => 1667),
array("title" =>"Animal Farm","authorId" => 5,"pubYear" => 1945),
array("title" =>"The Trial","authorId" => 1,"pubYear" => 1925));

foreach($books as $key=>$array){
if($authors[$array['authorId']]){
$books[$key]['authorName'] = $authors[$array['authorId']];
}   
 }
 echo "<pre>";print_r($books);

Output

Array
(
    [0] => Array
        (
            [title] => The Hobbit
            [authorId] => 2
            [pubYear] => 1937
            [authorName] => Tolkien
        )

    [1] => Array
        (
            [title] => The Grapes of Wrath
            [authorId] => 0
            [pubYear] => 1939
            [authorName] => Steinbeck
        )

    [2] => Array
        (
            [title] => A Tale of Two Cities
            [authorId] => 3
            [pubYear] => 1859
            [authorName] => Dickens
        )

    [3] => Array
        (
            [title] => Paradise Lost
            [authorId] => 4
            [pubYear] => 1667
            [authorName] => Milton
        )

    [4] => Array
        (
            [title] => Animal Farm
            [authorId] => 5
            [pubYear] => 1945
            [authorName] => Orwell
        )

    [5] => Array
        (
            [title] => The Trial
            [authorId] => 1
            [pubYear] => 1925
            [authorName] => Kafka
        )

)