Reputation: 145
I'm a newbie, can someone help me?
I have an array
array(2) {
["Peter"]=>
object(stdClass)#504 (2) {
["id"]=>
string(4) "2226"
["name"]=>
string(4) "Peter"
}
["Sam"]=>
object(stdClass)#505 (2) {
["id"]=>
string(4) "2227"
["name"]=>
string(14) "Sam"
}
}
I want to get element from the column "id" then put into the sql for looping
$idcol = array()
foreach($info as $item){
$idcol[] = $item['id'];
}
But I don't know why I cannot get the id column.
For Looping,How to put the array element of $idcol into the sql? get_sql is a function
for($i=0;$i<count($idcol);$i++){
$rate = get_sql($idcol);
}
Upvotes: 0
Views: 193
Reputation: 152
$idcol = array()
foreach($info as $item=>$val)
{
$idcol[] = $val['id'];
}
Upvotes: -1
Reputation: 2438
When you iterate through $info
in $item
you have object, not an array.
So you need to call it $item->id
if get_sql()
accept only one id you need to do something like
foreach ($idcol as $id){
$rate = get_sql($id);
}
but it is always bad idea to query SQL inside the loop. Consider to change your SQL query to something like
SELECT * from `table` where `id` IN (1, 2, 3);
than you will retrieve all data at once. But if you still want to call function you don't need two loops. Just do all stuff in one.
$idcol = array()
foreach($info as $item){
$rate = get_sql($item->id);
}
but note, that rate will be rewritten in each iteration. So you need to collect data to array like $rate[] = get_sql($item->id);
or process that data immediately like print it.
Upvotes: 1
Reputation: 54619
You can also use array_map
to get the ids:
$idcol = array_map(function($item){return $item->id;},$info);
Then you can use a foreach
loop to send the values to your function (though it would be best if you could make your function work with arrays to avoid multiple queries):
foreach($idcol as $id){
$rate = get_sql($id);
}
The reason your current loop doesn't work is because you're sending the whole array and not using an index like $idcol[i]
Upvotes: 0
Reputation: 10141
The reason you are not able to access the value form the array is that the details are stored as object, so you need to use arrow operator '->' to acces the values. So you can try the code as below :
$idcol = array()
foreach($info as $person_details)
{
$id = $person_details->id;
$person_name = $person_details->name;
$idcol[] = $id;
}
Upvotes: 2
Reputation: 1
Assuming that $info
is the array mentioned at the beginning, each $item
is an object, not an array. So you should try:
foreach($info as $item){
$idcol[] = $item->id;
}
Upvotes: 0
Reputation: 36
array_column — Return the values from a single column in the input array
Syntax Description
array array_column ( array $array , mixed $column_key [, mixed $index_key = null ] )
array_column() returns the values from a single column of the array, identified by the column_key. Optionally, you may provide an index_key to index the values in the returned array by the values from the index_key column in the input array.
Example:
<?php
// Array representing a possible record set returned from a database
$records = array(
array(
'id' => 2135,
'first_name' => 'John',
'last_name' => 'Doe',
),
array(
'id' => 3245,
'first_name' => 'Sally',
'last_name' => 'Smith',
),
array(
'id' => 5342,
'first_name' => 'Jane',
'last_name' => 'Jones',
),
array(
'id' => 5623,
'first_name' => 'Peter',
'last_name' => 'Doe',
)
);
$first_names = array_column($records, 'first_name');
print_r($first_names);
?>
Get column of last names from recordset, indexed by the "id" column
<?php
// Using the $records array from Example #1
$last_names = array_column($records, 'last_name', 'id');
print_r($last_names);
?>
Upvotes: -1