I want to find one key of array value in another key in the same array.
Following is my array output.
Array
(
[0] => Array
(
[id] => 1187
[user_id] => 168
[content] => Thanks a lot man
[item_id] => 1182
[secondary_item_id] => 1186
)
[1] => Array
(
[id] => 1186
[user_id] => 222
[content] => Great Post
[item_id] => 1182
[secondary_item_id] => 1182
)
[2] => Array
(
[id] => 1183
[user_id] => 185
[content] => Amazing first post
[item_id] => 1182
[secondary_item_id] => 1182
)
[3] => Array
(
[id] => 1184
[user_id] => 179
[content] => Wonder Post
[item_id] => 1182
[secondary_item_id] => 1182
)
[4] => Array
(
[id] => 1185
[user_id] => 168
[content] => Rocking Thanks
[item_id] => 1182
[secondary_item_id] => 1183
)
)
Here you can see id
& secondary_item_id
key in array, So I want to find which array id is used in secondary_item_id key of array or how can i search id key of array is used in secondary_item_id key in array.
For example.. you can see 'Great Post'
id key is used in 'Thanks a lot man'
secondary_item_id key of array. So i want to search which array key id is used in secondary_item_id key of array.
I have tried using following way but not working.
$commentData = array();
foreach ($commentQuery as $key => $value) {
if(array_search($value['id'], array_column($commentQuery, 'secondary_item_id'))){
echo "list Found".$value['id'];
}else{
echo "list not Found".$value['id'];
}
}
It's only return list not Found1187
.
i hope this works for you
$ch=true;
foreach ($commentQuery as $key => $value) {
foreach ($commentQuery as $key2 => $value2) {
if($value2['id']==$value['secondary_item_id']){
$ch=false;
echo "list Found ".$value['id']." in array key ".$key2;
break;
}
}
if($ch){
echo "list not Found".$value['id'];
}
$ch=true;
}
Try this:-only single foreach loop using
$search_column variable is out of foreach because every time this sentence execute and your code execute slowly
$search_column = array_column($commentQuery, 'secondary_item_id');
foreach ($commentQuery as $key => $value) {
$search = null;
$search = array_search($value['id'], $search_column);
if (gettype($search) == 'integer') {
echo "list Found" . $value['id'];
} else {
echo "list not Found" . $value['id'];
}
echo "<br>";
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.