How can i get the arrays from the JSON file Dynamically

前端 未结 1 1624
盖世英雄少女心
盖世英雄少女心 2021-01-17 06:24

I have Json file which contain alot of Arrays. How can i get the the array Dynamically for each person by name like the get Parameter.

JSON

相关标签:
1条回答
  • 2021-01-17 06:44

    I don't know what exactly you need to do (find only one match, or fetch all), but this is for both :

    <?php
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    
    $array = array(
    1 => array( "Name" => "Somename1", "Lastname" => "somelastname1", "Address" => "someaddress1"),
    2 => array( "Name" => "Somename2", "Lastname" => "somelastname2", "Address" => "someaddress2"),
    3 => array( "Name" => "Somename3", "Lastname" => "somelastname3", "Address" => "someaddress3"),
    4 => array( "Name" => "Somename4", "Lastname" => "somelastname4", "Address" => "someaddress4")
    );
    
    $data1 = json_encode($array);
    //var_dump($data1);
    
    $data = json_decode($data1, true);
    //var_dump(json_decode($data1));
    
    $myname = "Somename3";
    
    /* one liner to get one result */
    
    if( array_search("$myname", array_column($data, 'Name')) == true ){ echo"[ $myname found ! ]"; } else { echo"[ no data match ! ]"; }
    
    /* loop to get all results */
    
    foreach($data as $user){
    echo $user['Name'].' '.$user['Lastname'].' '.$user['Address'].'<br/>'; 
    }
    
    ?>
    
    0 讨论(0)
提交回复
热议问题