1

I am not sure if I would make any sense in asking this but I have a model:

function update_profile_image($id, $image, $data){
    $data1=array('profile_thumb'=> $data['images_urls'][$index]);
    $this->db->where('property_ref_id',$id);
    $this->db->update('vbc_property_images',$data1);
}

Here $data is an array:

Array
(
[images_urls] => Array
    (
        [0] => property_image_11.png
        [1] => property_image_13.png
        [2] => property_image_14.png
    )

)

And $image in model is the name of any image in this array, e.g. 'property_image_13.png'.

I am trying to do something where I can get key value like $index([0], [1]..) by $image so that in my query it will automatically detect which image was selected.

Please help.

2 Answers 2

2

You can loop on the array to get the key of the image

foreach($data['images_urls'] as $key => $value) {
   if($value == $image) {
       $index = $key;
       break;
   }
}

Alternative solution is to use array_search()

$index = array_search($image, $data['images_urls']);
Sign up to request clarification or add additional context in comments.

Comments

0

If I understand your question correctly, you want to search $data for the value defined in $image, and return the key of the matching element. If so, array_search is what you need.

$key = array_search($image, $data);

Description from manpage:

Searches the array for a given value and returns the corresponding key if successful

2 Comments

Thanks @rjdown... I tried to print_r($key);.. doesn't echo anything. While $image echo image name
It should at the very least return false, if no matches are found. Try var_dump instead of print_r

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.