0

I have a result array being returned from a search function in one of the models. Everything works fine, however, I have recently added an iteration block to redact a row of data based on an ID. Like so:

for($i=0;$i<count($searchResponse);$i++){
    if($searchResponse[$i]['id']==$user_id){
        unset($searchResponse[$i]);
    }
}   

The code works fine and removes the appropriate data correctly. However it changes the array structure from this:

 "data":[
{
"id":"1",
"name_first":"Cameron",
"name_last":"Leafe",
"industry":"IT",
"workplace":"Leafe Interactive",
"imageURL":"https:\/\/scontent-b-kul.xx.fbcdn.net\/hphotos-xfa1\/t31.0-8\/p720x720\/819309_4979120151286_1476247244_o.jpg"
},
{
"id":"8",
"name_first":"Caroline",
"name_last":"McCullough",
"industry":"IT",
"workplace":"Occupation",
"imageURL":"http:\/\/www.mothersdelight.com\/avatar\/avatar.jpg"
},
{
"id":"11",
"name_first":"Akiko",
"name_last":"McDougall",
"industry":"IT",
"workplace":"Occupation",
"imageURL":"http:\/\/www.mothersdelight.com\/avatar\/avatar.jpg"
}
]

to this:

"data":{
"1":{
"id":"3",
"name_first":"Jane",
"name_last":"Doe",
"industry":"Retail - Fashion",
"workplace":"Big-W",
"imageURL":"http:\/\/www.johndoe.pro\/img\/John_Doe.jpg"
},
"5":{
"id":"8",
"name_first":"Caroline",
"name_last":"McCullough",
"industry":"IT",
"workplace":"Occupation",
"imageURL":"http:\/\/www.mothersdelight.com\/avatar\/avatar.jpg"
},
"6":{
"id":"11",
"name_first":"Akiko",
"name_last":"McDougall",
"industry":"IT",
"workplace":"Occupation",
"imageURL":"http:\/\/www.mothersdelight.com\/avatar\/avatar.jpg"
}
}

Note the added index for each item in the second example. This small change causes all sorts of problems client side, PHP is not my normal area and I am not sure why this block is changing the structure. Usually I would work around this on the client side, however, the product is already deployed.

Any insights or suggestions on how to prevent the iteration block from causing this integer index to appear would be greatly appreciated!

6
  • 1
    If you need to rearrange keys - use array_values after unsetting loop Commented Sep 9, 2014 at 6:02
  • Ordering is not the issue, the issue is each item in the array is getting numbered after it is processed through the iterator. This numbering screws with the consumer. Commented Sep 9, 2014 at 6:05
  • 1
    So if you have array with indexes [0,1,2] after unsetting index 1 you will have array with indexes [0,2], after using array_values indexes will be [0,1] while values won't change. Commented Sep 9, 2014 at 6:08
  • The second example has a different entry added to it in place of the first one -- is that deliberate? It makes your examples unnecessarily confusing. Commented Sep 9, 2014 at 6:23
  • @CBroe That's actually the problem the OP mentions. Commented Sep 9, 2014 at 6:48

1 Answer 1

1

You can use array_splice to manipulate non-associative arrays safely:

for($i=0;$i<count($searchResponse);$i++){
    if($searchResponse[$i]['id']==$user_id){
        array_splice($searchResponse, $i, 1);
    }
}

array_splice takes the arguments

array_splice( $array, $offset, $length*, $replacementArr* )

with the second two arguments being optional. In this case, we want to remove one element from the array at index $i, so we use

array_splice( $searchResponse, $i, 1 );

Splicing can also be used to chop up arrays or replace array elements with another array, so it's a handy function to know.

Sign up to request clarification or add additional context in comments.

3 Comments

How exactly array_splice in this case is better than using array_values on the sparsed array?
@raina77ow: TMTOWTDI.
Spot on mate, appreciate it!

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.