0

D

Could you give a an advice how to insert keys/index to a an array in a specific order as you can se.... I would like to in the result index the values id1 = 3 id2 = 4 id3 = 5. How do I do that? This code

public static function getTest($ids){
    $input = array();
    foreach ($ids as $id) {
        $input['result'] = $ids;
    }

    $result = array('status'=>"success",
                    'message'=>"blah blah",
                    'result'=> $ids
              );
    var_dump($result);
    return $result;
}

produces this (getTest is called from another file and it gives out array(3,4,5))

array(3) { 
    ["status"]=> string(7) "success" 
    ["message"]=> string(9) "blah blah" 
    ["result"]=> array(3) { 
        [0]=> int(3) 
        [1]=> int(4) 
        [2]=> int(5) 
    }
} 
2
  • What is $input['result'] for? Commented Sep 27, 2011 at 11:09
  • that was me trying to make it work...it gives out the mentioned output without all of this $input =array( ); foreach ($ids as $id) { $input['result']=$ids; } Commented Sep 27, 2011 at 11:12

1 Answer 1

1

If you create an array the elements are in the order you added them:

$a = array();
$a[2] = 2;
$a[1] = 1;
$a[9] = 9;
var_dump($a);
// array(3) { [2]=> int(2) [1]=> int(1) [9]=> int(9) }

I am not really sure what you want but have a look here the page always helps my lot.

Response to comment: Try this

foreach ($ids as $key => $id) {
    $input['result']['ID'.$key] =  $id;
}
Sign up to request clarification or add additional context in comments.

1 Comment

I have this getTest::(array(3,4,5)); in the controller file, and it gives out the array to the get test function in an Api class. I want to getTest to give me an array in a orderly fashion back to the controller. Like this array(3) { ["status"]=> string(7) "success" ["message"]=> string(9) "blah blah" ["result"]=> array(3) { [ID1]=> int(3) [ID2]=> int(4) [ID3]=> int(5) } }

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.