I just want to make sure this is what you want.
This:
$cid[] ='';
allocates an array.
$cid[] ='';
print_r( $cid );
-----------------------------output-----------------------------------
Array
(
[0] =>
)
This:
foreach( $example as $value ) {
$cid = $value -> id_In_Restaurant;
};
assign the $cid
to the last element of example's id_In_Restaurant. I have no idea what that would be, let's say it's 4.
print_r($cid);
-----------------------------output-----------------------------------
Array
(
[cid] => 4
)
If you want that, don't allocated $cid as an array just do $cid = $example[count($example)-1];
.
If you don't want that and want to append to the array, you need to put brackets after $cid
in the for loop.
foreach( $example as $value ) {
$cid[] = $value-> id_In_Restaurant;
};
The next part gets tricky depending on how you want your data to come out. If you want every id in the array to be indexed as 'cid', you can't because it would cause a collision. To get around this, you can make an array of arrays.
foreach( $example as $value ) {
$cid[] = ['cid' => $value-> id_In_Restaurant];
};
echo json_encode( $cid );
-----------------------------output-----------------------------------
Array
(
[0] =>
[1] => Array
(
[cid] => 1
)
[2] => Array
(
[cid] => 2
)
[3] => Array
(
[cid] => 3
)
[4] => Array
(
[cid] => 4
)
[5] => Array
(
[cid] => 7
)
[6] => Array
(
[cid] => 95
)
[7] => Array
[cid] => 394
)
[8] => Array
(
[cid] => 23156
)
[9] => Array
(
[cid] => 4
)
)
But if you want to assign the array of all the ids to "cid" you can do
foreach( $example as $value ) {
$cid[] = $value-> id_In_Restaurant;
};
echo json_encode( [ 'cid' => $cid ]);
-----------------------------output-----------------------------------
{"cid":["",1,2,3,4,7,95,394,23156,4]}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…