php


Search in array in json file


I have an array which has a key with multiple content. I want to get that array which includes the key that I search .
$arr = json_decode('{"people":[
{
"id": "8080",
"content": "foo",
"member": [123, 456],
"interval": 7
},
{
"id": "8097",
"content": "bar",
"member": [1234, 4567],
"interval": 7
}
]}', true);
$results = array_filter($arr['people'], function($people) {
return $people['id'] == 8080;
});
echo json_encode($results);
This will return:
{"id":"8080","content":"foo","member":[123,456],"interval":7}
I want that:
$results = array_filter($arr['people'], function($people) {
return $people['member'] == 123;
});
And this does not work.
Have somebody an idea?
As #JonStirling said in comment. Use in_array() function.
$arr = json_decode('{"people":[
{
"id": "8080",
"content": "foo",
"member": [123, 456],
"interval": 7
},
{
"id": "8097",
"content": "bar",
"member": [1234, 4567],
"interval": 7
}
]}', true);
$searchId = 123;
$results = array_filter($arr['people'], function($people) use ($searchId) {
return in_array($searchId, $people['member']);
});
echo json_encode($results);
Result:
[{"id":"8080","content":"foo","member":[123,456],"interval":7}]
See if this helps:
$arr = json_decode('{"people":[
{
"id": "8080",
"content": "foo",
"member": [123, 456],
"interval": 7
},
{
"id": "8097",
"content": "bar",
"member": [1234, 4567],
"interval": 7
}
]}', true);
$results = array_filter($arr['people'], function($people) {
for($i=0; $i<count($people['member']); $i++){
return $people['member'][$i] == 123;
}
});
echo json_encode($results);
The out come will be:
[{"id":"8080","content":"foo","member":[123,456],"interval":7}]
If you want to do it withouth 'array_filter' you can try this:
function search($arr, $id, $arrayValue)
{
$people = null;
foreach ($arr['people'] as $a)
{
if ($a['id'] == $id)
{
$people = $a;
}
}
$arrayWeAreLookingFor = null;
foreach ($people as $property => $value)
{
if (is_array($value))
{
foreach ($value as $v)
{
if ($v == $arrayValue)
{
$arrayWeAreLookingFor = $people[$property];
}
}
}
}
return $arrayWeAreLookingFor;
}
var_dump(search($arr, 8080, 123));

Related Links

Make <br> optional for the last line
Catchable Fatal Error: Argument 1 passed to \Entity\Image::setFile() must be an instance of Symfony\Component\HttpFoundation\File\UploadedFile,
PayPal IPN with standart button says that result is invalid
CMSimple_XH PHP 5.5
xCode push notifications. Size of array has non-integer type
saving a photo or file in symfony 1.4 action
PHP: how parse a simil - XML string? [duplicate]
My order form keeps sending me to the error page when I test it
Class 'Facebook\GraphObject' not found with Facebook PHP SDK 4
socket_bind(): unable to bind address
PHP script not echoing data when called via AJAX
Check extensions of files in a directory and do an action for a specific file using php
phpMailer not working after being hosted [duplicate]
Is it possible to use new object directly? i.e. “new foo()->zork()” [duplicate]
What is good practise to output data with double quotes " in php? [duplicate]
Code is not displaying first result

Categories

HOME
cocoapods
opengl
protocol-buffers
memory
macos-sierra
gaming
apple-push-notifications
selenium-builder
hid
celery
lvm
dependencies
google-search-console
minecraft
tweepy
kurento
tfs2010
roku
siesta
buffer
mailmerge
cuba-platform
closures
tomcat8
http-authentication
pfobject
connection-refused
uri
offline-caching
running-object-table
marquee
php-5.3
source-insight
autodesk-model-derivative
windowsiot
steam-web-api
riotjs
actframework
plantuml
apptentive
transform
cups
jade4j
eclipse-gmf
sspi
android-n
google-now
best-buy-api
klee
system-on-chip
android-webservice
http-basic-authentication
dtd
predicates
nrf51
email-injection
jsf-2.3
raw-sockets
cdo.message
android-4.2-jelly-bean
android-sdcard
android-syncadapter
uibinder
jeasyui
swiffy
textblock
as3-api
django-validation
odesk
astropy
mixins
libz
textscan
wicked-gem
whitespace-language
sablecc
subview
html-escape-characters
fat32
symja
datagridcomboboxcolumn
utf8-decode
cout
zend-mail
berkeley-db-xml
tabris
xhprof
usb-flash-drive
armv6
saga
ldif
acceptance-testing
sunstudio
unicoins
popup-blocker
jdom
series-40
autosize
exponent
uipasteboard
directshow.net
downgrade
ninject-interception

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App