How to get nested array data from key value pair in php ?

Suppose we have data in JSON format. Inside it we have an array and inside this array we have data in sub array format. Hence to access a particular key value data we need to use foreach loop. For example please see the below code.

Below data in JSON format.

$json = [{"name":"q","value":""},{"name":"options[prefix]","value":"last"},{"name":"form_type","value":"create_customer"},{"name":"customer[first_name]","value":"customer1_fname"},{"name":"customer[last_name]","value":"customer1_lname"},{"name":"customer[email]","value":"[email protected]"},{"name":"customer[password]","value":"customer1password"}]

Now if we want to get a particular value on the basis of key with if condition so we need to do the following code. Before that we need to decode data by following code.

$data_array = json_decode($json);

Our JSON data has been now decoded. Now we can go ahead. Now use the below logic to get only customer email ID.

foreach($data_array as $valarray) {
            if($valarray['name'] == "customer[email]")
              echo $valarray['value'];

That’s it……

A very simple logic but its very useful for fetch a particular data from JSON data.