0

I have a script that creates an array in the following format

  $named_array["vehicles"][0]['vehicle'] = "i100-1 "  ;
  $named_array["vehicles"][1]['vehicle'] = "i100-2 "  ;
  $named_array["vehicles"][2]['vehicle'] = "i100-46 "  ;

What I want to do later in the script is get the index value[0-1-2 etc] from $named_array but I only have the value ( i100-1 etc) as a query option , This is so I can alter it later. What I want to achieve is something like , what is the index value of $named_array where value is i100-2

this is output to json at the end .

I hope this makes sense ! any help please ?

2
  • 1
    I really don't understand what your desired result is. I also don't see why you need to add another dimension for vehicle in an array of vehicles. Commented Mar 21, 2013 at 18:19
  • Prince I want a vehicle array in vehicles that's why Commented Mar 21, 2013 at 18:53

2 Answers 2

2
function complex_index_of($named_array, $val){
    for($i=0, $n=count($named_array['vehicles']); $i<$n; $i++){
        if ($named_array['vehicles'][$i]['vehicle'] == $val)
            return $i;
    }
    return -1;
}


echo complex_index_of($named_array, 'i100-2 ');
// output: 1
Sign up to request clarification or add additional context in comments.

Comments

1

Try something like this (maybe create a function, if you need to do it more than once)

$needle = 'i100-1';
$vIndex = -1;
foreach ($named_array["vehicles"] as $index => $data) {
    if($data['vehicle'] == $needle) {
        $vIndex = $index;
        break;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.