0

First, im new in PHP so please bear with me.

I just want to add an array with the foreach loop but I can't.

if($size > 0)
    {
        $resultArray = array();

        foreach($result as $hospital)
        {
            if($hospital)
            {

                $temp = array('result' => 'true', 
                'total' => $size,
                'id' => $hospital['id'], 
                'name' => $hospital['name'], 
                'address' => $hospital['address'], 
                'telp' => $hospital['telp']);

                $resultArray = array_merge_recursive($resultArray, $temp);
                //$resultArray = array_splice($resultArray, $i, 0, $temp);
            }
        }

        $this->response($resultArray, 200);
    }

I tried to create a $temp array and merge it to the final result, and finally print that final result ($resultArray) to the response.

The array is successfully merged, but not in the way i want. This is the JSON result :

{"result":["true","true"],"total":[2,2],"id":["1","2"],"name":["test","keyword"],"address":["alamat test","alamat keyword"],"telp":["123456","789"]}

What i want is something like this :

-0 :{
result: "true"
total: 2
id: "1"
name: "test"
address: "alamat test"
telp: "123456"
}

-1 :{
result: "true"
total: 2
id: "2"
name: "test2"
address: "alamat tes 2t"
telp: "789"
}

So the response should be an array with 2 items, and each items has some items.

Please kindly help me, Thanks for your help.

3 Answers 3

1

It looks to me like you're trying to append an array, to an array, which can be done quite easily:

$resultArray = array();//outside the loop
//loop
$resultArray[] = array(
    'result' => 'true', 
    'total' => $size,
    'id' => $hospital['id'], 
    'name' => $hospital['name'], 
    'address' => $hospital['address'], 
    'telp' => $hospital['telp']
);

Job done. You could also use the array_push function, but that's a function. Functions equal overhead, and should, therefore be avoided (if it doesn't affect code readability). You use array_merge and array_merge_recursive if you want to combine arrays:

$foo = array(
    array('bar' => 'zar')
);
$bar = array(
    array('car' => 'far')
);
var_dump(
    array_merge(
        $foo,
        $bar
    )
);

The effect of array_merge here is comparable to:

foreach ($bar as $subArray)
    $foo[] = $subArray;
Sign up to request clarification or add additional context in comments.

2 Comments

Yesss, this is what i mean, sorry for not using "append" word in the question. I cant accept now, need 4 more minutes
Your answer is the best. And thanks for detailed explanation, very helpful for me
1

Im not totally sure how you want your final array to be, but this will put it like $resultArray[0] = array( //temparray ) etc..:

if($size > 0){
    $resultArray = array();
    $i=0;
    foreach($result as $hospital){
        if($hospital){
            $temp = array('result' => 'true', 
            'total' => $size,
            'id' => $hospital['id'], 
            'name' => $hospital['name'], 
            'address' => $hospital['address'], 
            'telp' => $hospital['telp']);
            $resultArray[$i][$temp];
            $i++;
        }
    }
    $this->response($resultArray, 200);
}

1 Comment

Yes, your solution is working. Thanks a lot. Its too bad i only can accept 1 question and 1 upvote.
1

Replace this -

$resultArray = array_merge_recursive($resultArray, $temp);

With this -

$resultArray[] = $temp;

1 Comment

i think we have some miss-com, but i fixed it already. Thanks a lot

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.