0

I've been using the following to build a clause for multiple deletes at once which works great. This was for single dimension arrays though.

$prefix = $in_clause = '';
$binding_clause = array();  
foreach($selected as $key => $value)
{
    $in_clause .= $prefix.':selected_'.$key;
    $prefix = ', ';
    $binding_clause[':selected_'.$key] = $value;
}

Now, I need to use it on a specific value of a multidimensional array. Given this :

Array
(
    [0] => Array
        (
            [account_id] => 2
            [screenshot_id] => 120262
            [image_filename] => a1.jpg
        )

    [1] => Array
        (
            [account_id] => 2
            [screenshot_id] => 120263
            [image_filename] => a2.jpg
        )
    .......

I only need the key value and the screenshot_id value so I am trying to change the first function so the values are :

key = 0, value = 120262
key = 1, value = 120263

Hopefully that makes sense. Not sure how to do this with a multi-array.

2 Answers 2

1

Since the array elements are arrays, use array indexing to access that part of the elements:

foreach ($selected as $key => $value) {
    $binding_clause[':selected_'.$key] = $value['screenshot_id'];
}
Sign up to request clarification or add additional context in comments.

1 Comment

That's it. Guess I was overthinking it there as I didn't think value would take the array. Thanks!
1

Assuming you have the array format as explained above.

for($i=0; $i<=$array.count(); $i++){
    foreach($array[$i] as $key => $value){
        echo "Key: ". $i . " Value:" . $key['screenshot_id']
    }
}

1 Comment

You don't need the nested loop.

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.