BV45
BV45

Reputation: 605

Getting error T_DOUBLE_ARROW for multi dimensional associative array

I am testing out a recursive PHP script. But the associative array beginning with "items" key is throwing back the error T_DOUBLE_ARROW. Do I need to use brackets for this portion? The "methods" key is a layer within the "items" keys. Can someone guide me to how I can fix this error? The find_in_arr function works fine when calling name, subject and type keys. But when it gets to items I get the error.

<?php
function find_in_arr($key, $arr) 
{
foreach ($arr as $k => $v) 
{
    if ($k == $key) 
    {
        return $v;
    }       
    if (is_array($v)) 
    {
        foreach ($v as $_k => $_v) 
        {
        if ($_k == $key)
        {
                return $_v;
            }
        }
    }
}
return false;
}

$arr = 
array(
"name" => "Php Master",
"subject" => "Php",
"type" => "Articles",
"items" => ("one" => "Iteration","two" => "Recursion",
"methods" => ("factorial" => "Recursion","fibonacci" => "Recursion"),)
"parent"? => "Larry Ullman",
 );

var_dump
(
find_in_arr('two', $arr),
find_in_arr('parent', $arr),
find_in_arr('fibonacci', $arr)
//find_in_arr('name', $arr),
//find_in_arr('subject', $arr),
//find_in_arr('type', $arr)
);

Upvotes: 0

Views: 65

Answers (1)

Sharanya Dutta
Sharanya Dutta

Reputation: 4021

It should be:

$arr = array(
    "name" => "Php Master",
    "subject" => "Php",
    "type" => "Articles",
    "items" => array(
        "one" => "Iteration",
        "two" => "Recursion",
        "methods" => array(
            "factorial" => "Recursion",
            "fibonacci" => "Recursion"
         )
    ),
    "parent" => "Larry Ullman"
);

Upvotes: 1

Related Questions