How can i get the arrays from the JSON file Dynamically

前端 未结 1 1627
盖世英雄少女心
盖世英雄少女心 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 :

     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'].'
    '; } ?>

    0 讨论(0)
提交回复
热议问题