Skip to content
Advertisement

How do i loop this array? [closed]

    $datadiri = array(
        "Bimo Nasuti" => array (
            "mob" => "6545444455",
            "email" => "bimonasuti@gmail.com",
            "hp" => "08123456787",

        ),
        "Bimo Nasuti" => array (
            "mob" => "6545444455",
            "email" => "bimonasuti@gmail.com",
            
        ),
        "Yuni Salam" => array (
            "mob" => "512512662",
            "email" => "yunisalam@gmail.com",
            "hp" => "08123456787",

        )
    );
?>

must be looped, on condition that if you don’t have “hp” then it will be labeled “No Phone Number

Advertisement

Answer

Check this

foreach($datadiri as $data) {
    if(!array_key_exists('hp', $data)) {
        echo 'No Phone Number' . PHP_EOL;
    } else {
        echo $data['hp'] . PHP_EOL;
    }
}

Return:

No Phone Number
08123456787

In your array you have twice the same key.

"Bimo Nasuti" => array (

You should use some unique ID.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement