/    Sign up×
Community /Pin to ProfileBookmark

Search multidimensional array and return new array

Hi all

I’m stuck on this an i’m hopping to get some help.

I need to search an array of albums and videos (videos are child arrays of the album) for a value (in my case a tag) and then return the ‘video’ of the matched value as a new array (Not separated by albums).

This is a small example of how my main array is formatted (actual array contains over 20 albums and each album contains between 3 and 10 videos. Each video contains 4+ tags):

[code=php]
Array
(
[0] => Array
(
[id] => 123456
[title] => Album 1 title
[description] => This is my ALBUM description
[url] => http:/example.com
[total] => 2
[thumb] => http:/example.com/image-thumb.jpg
[videos] => Array
(
[0] => Array
(
[id] => 345678
[title] => Video 1 title
[privacy] => anywhere
[url] => http:/example.com
[album] => Album 1 title
[description] => This is my VIDEO description
[tags] => Array
(
[0] => tag_0
[1] => tag_1
[2] => tag_2
)

[thumbs] => Array
(
[0] => http:/example.com/image-thumb0.jpg
[1] => http:/example.com/image-thumb1.jpg
[2] => http:/example.com/image-thumb2.jpg
[3] => http:/example.com/image-thumb3.jpg
)

)

[1] => Array
(
[id] => 456789
[title] => Video 2 title
[privacy] => anywhere
[url] => http:/example.com
[album] => Album 1 title
[description] => This is my VIDEO description
[tags] => Array
(
[0] => tag_2
[1] => tag_3
[2] => tag_4
)

[thumbs] => Array
(
[0] => http:/example.com/image-thumb0.jpg
[1] => http:/example.com/image-thumb1.jpg
[2] => http:/example.com/image-thumb2.jpg
[3] => http:/example.com/image-thumb3.jpg
)

)
)

)

[1] => Array
(
[id] => 234567
[title] => Album 2 title
[description] => This is my ALBUM description
[url] => http:/example.com
[total] => 2
[thumb] => http:/example.com/image-thumb.jpg
[videos] => Array
(
[0] => Array
(
[id] => 567890
[title] => Video 3 title
[privacy] => anywhere
[url] => http:/example.com
[album] => Album 2 title
[description] => This is my VIDEO description
[tags] => Array
(
[0] => tag_5
[1] => tag_4
[2] => tag_3
)

[thumbs] => Array
(
[0] => http:/example.com/image-thumb0.jpg
[1] => http:/example.com/image-thumb1.jpg
[2] => http:/example.com/image-thumb2.jpg
[3] => http:/example.com/image-thumb3.jpg
)

)

[1] => Array
(
[id] => 567895
[title] => Video 4 title
[privacy] => anywhere
[url] => http:/example.com
[album] => Album 2 title
[description] => This is my VIDEO description
[tags] => Array
(
[0] => tag_2
[1] => tag_3
[2] => tag_4
)

[thumbs] => Array
(
[0] => http:/example.com/image-thumb0.jpg
[1] => http:/example.com/image-thumb1.jpg
[2] => http:/example.com/image-thumb2.jpg
[3] => http:/example.com/image-thumb3.jpg
)

)
)

)
)
[/code]

for example I would like to search for all videos that contain the value [B][COLOR=”Red”](tag_2)[/COLOR][/B] in the child array with the key [COLOR=”RoyalBlue”][B][tags][/B][/COLOR] and return a new array with JUST the matched videos (with all details)in one array.

Hope this makes sense. Would be good if tis was a function that would be quite robust if the array structure did for some reason change slightly.

[COLOR=”Red”][B]tag_2[/B][/COLOR] was used about just as an example. I will need to set the search term in the function.

eg:

[code=php]$search_term = ‘tag_2’;

search_my_array($search_term, $my_array)[/code]

This would be the returned array if I searched [COLOR=”Red”][B]tag_2[/B][/COLOR]

[code=php]Array
(
[0] => Array
(
[id] => 345678
[title] => Video 1 title
[privacy] => anywhere
[url] => http:/example.com
[album] => Album 1 title
[description] => This is my VIDEO description
[tags] => Array
(
[0] => tag_0
[1] => tag_1
[2] => tag_2
)

[thumbs] => Array
(
[0] => http:/example.com/image-thumb0.jpg
[1] => http:/example.com/image-thumb1.jpg
[2] => http:/example.com/image-thumb2.jpg
[3] => http:/example.com/image-thumb3.jpg
)

)

[1] => Array
(
[id] => 456789
[title] => Video 2 title
[privacy] => anywhere
[url] => http:/example.com
[album] => Album 1 title
[description] => This is my VIDEO description
[tags] => Array
(
[0] => tag_2
[1] => tag_3
[2] => tag_4
)

[thumbs] => Array
(
[0] => http:/example.com/image-thumb0.jpg
[1] => http:/example.com/image-thumb1.jpg
[2] => http:/example.com/image-thumb2.jpg
[3] => http:/example.com/image-thumb3.jpg
)

)

[3] => Array
(
[id] => 567895
[title] => Video 4 title
[privacy] => anywhere
[url] => http:/example.com
[album] => Album 2 title
[description] => This is my VIDEO description
[tags] => Array
(
[0] => tag_2
[1] => tag_3
[2] => tag_4
)

[thumbs] => Array
(
[0] => http:/example.com/image-thumb0.jpg
[1] => http:/example.com/image-thumb1.jpg
[2] => http:/example.com/image-thumb2.jpg
[3] => http:/example.com/image-thumb3.jpg
)

)
)
[/code]

Any help is much appreciated.

Cheers
C

to post a comment
PHP

13 Comments(s)

Copy linkTweet thisAlerts:
@NogDogAug 11.2011 — Untested, just a quick something to give you an idea:
[code=php]
$matches = array();
foreach($data as $album) {
foreach($album as $video) {
if(in_array($tagBeingSearched, $video) {
$matches[] = $video;
}
}
}
echo "<pre>".print_r($matches, true)."</pre>";
[/code]
Copy linkTweet thisAlerts:
@cybercampbellauthorAug 11.2011 — Thanks Nog Dog

there was a missing bracket on line 4 but even when I put that in I didn't get any results.

just this:

[code=php]Array
(
)
Array
(
)
Array
(
)[/code]


Got the 3 empty arrays even when the there was no search term.

Any ideas?
Copy linkTweet thisAlerts:
@cybercampbellauthorAug 11.2011 — ok.. i think i worked this out. It seems to be working.

Can you see anything wrong with this:

[code=php]$matches = array();

foreach($albumInfo as $album) {
foreach($album['videos'] as $video) {
if(in_array($searchTag, $video['tags'])) {
$matches[] = $video;
}
}
}
echo "<pre>".print_r($matches, true)."</pre>"; [/code]
Copy linkTweet thisAlerts:
@cybercampbellauthorAug 11.2011 — actually... thats not right and not working... I feel it should be this:

[code=php]foreach($albumInfo as $album) {
foreach($album['videos'] as $video) {
if(in_array($searchTag, $video)) {
$videoMatches[] = $video;
}
}
}[/code]


But this doesn't return any results

Ideas?
Copy linkTweet thisAlerts:
@cybercampbellauthorAug 11.2011 — ok.. I read that in_array doesn't support multidimensional arrays so I found a recursive function.

I know have this but it only returns 2 results even though I know it should return more.

[code=php]$searchTag = 'My Tag';

$videoMatches = array();

function in_array_r($needle, $haystack) {
foreach ($haystack as $item) {
if ($item === $needle || (is_array($item) && in_array_r($needle, $item))) {
return true;
}
}
return false;
}

foreach($albumInfo as $album) {
foreach($album['videos'] as $video) {
if(in_array_r($searchTag, $video)) {
$videoMatches[] = $video;
}
}
}[/code]


Any ideas?
Copy linkTweet thisAlerts:
@cybercampbellauthorAug 11.2011 — Also need to make sure it only matches in the tags array and not any other.

eg:

wouldn't want it to match the word cybercampbell in the album name:

[code=php][0] => Array
(
[id] => 345678
[title] => Video 1 title
[privacy] => anywhere
[url] => http:/example.com
[album] => cybercampbell
[description] => This is my VIDEO description
[tags] => Array
(
[0] => tag_0
[1] => cybercampbell
[2] => tag_2
)

[thumbs] => Array
(
[0] => http:/example.com/image-thumb0.jpg
[1] => http:/example.com/image-thumb1.jpg
[2] => http:/example.com/image-thumb2.jpg
[3] => http:/example.com/image-thumb3.jpg
)

) [/code]
Copy linkTweet thisAlerts:
@NogDogAug 11.2011 — This seems to work:
[code=php]
<?php
/**
* The search function
* @return array
* @param array $arr
* @param string $tag
*/
function searchArrayByTag($arr, $tag)
{
$result = array();
foreach($arr as $item) {
foreach($item['videos'] as $video) {
if(in_array($tag, $video['tags'])) {
$result[] = $video;
}
}
return $result;
}
}

$data = array(
'0' => array(
'id' => '123456',
'title' => 'Album 1 title',
'description' => 'This is my ALBUM description',
'url' => 'http:/example.com',
'total' => '2',
'thumb' => 'http:/example.com/image-thumb.jpg',
'videos' => array(
'0' => array(
'id' => '345678',
'title' => 'Video 1 title',
'privacy' => 'anywhere',
'url' => 'http:/example.com',
'album' => 'Album 1 title',
'description' => 'This is my VIDEO description',
'tags' => array(
'0' => 'tag_0',
'1' => 'tag_1',
'2' => 'tag_2',
) ,
'thumbs' => array(
'0' => 'http:/example.com/image-thumb0.jpg',
'1' => 'http:/example.com/image-thumb1.jpg',
'2' => 'http:/example.com/image-thumb2.jpg',
'3' => 'http:/example.com/image-thumb3.jpg',
)
) ,
'1' => array(
'id' => '456789',
'title' => 'Video 2 title',
'privacy' => 'anywhere',
'url' => 'http:/example.com',
'album' => 'Album 1 title',
'description' => 'This is my VIDEO description',
'tags' => array(
'0' => 'tag_2',
'1' => 'tag_3',
'2' => 'tag_4'
) ,
'thumbs' => array(
'0' => 'http:/example.com/image-thumb0.jpg',
'1' => 'http:/example.com/image-thumb1.jpg',
'2' => 'http:/example.com/image-thumb2.jpg',
'3' => 'http:/example.com/image-thumb3.jpg'
)
)
)
) ,
'1' => array(
'id' => '234567',
'title' => 'Album 2 title',
'description' => 'This is my ALBUM description',
'url' => 'http:/example.com',
'total' => '2',
'thumb' => 'http:/example.com/image-thumb.jpg',
'videos' => array(
'0' => array(
'id' => '567890',
'title' => 'Video 3 title',
'privacy' => 'anywhere',
'url' => 'http:/example.com',
'album' => 'Album 2 title',
'description' => 'This is my VIDEO description',
'tags' => array(
'0' => 'tag_5',
'1' => 'tag_4',
'2' => 'tag_3'
) ,
'thumbs' => array(
'0' => 'http:/example.com/image-thumb0.jpg',
'1' => 'http:/example.com/image-thumb1.jpg',
'2' => 'http:/example.com/image-thumb2.jpg',
'3' => 'http:/example.com/image-thumb3.jpg'
)
) ,
'1' => array(
'id' => '567895',
'title' => 'Video 4 title',
'privacy' => 'anywhere',
'url' => 'http:/example.com',
'album' => 'Album 2 title',
'description' => 'This is my VIDEO description',
'tags' => array(
'0' => 'tag_2',
'1' => 'tag_3',
'2' => 'tag_4',
) ,
'thumbs' => array(
'0' => 'http:/example.com/image-thumb0.jpg',
'1' => 'http:/example.com/image-thumb1.jpg',
'2' => 'http:/example.com/image-thumb2.jpg',
'3' => 'http:/example.com/image-thumb3.jpg',
)
)
)
)
);
$test = searchArrayByTag($data, 'tag_2');
echo "<pre>".print_r($test, true)."</pre>";
[/code]
Copy linkTweet thisAlerts:
@cybercampbellauthorAug 11.2011 — hmmm works with your array but not mine... I only get results from the first album. Strange thing is my array is the exact same format as the example one I used here.

Can I PM you my actual array (serialized in a txt doc)?

I can't post it here as it has sensitive information in it.
Copy linkTweet thisAlerts:
@NogDogAug 12.2011 — It basically still worked for me, but I found an issue with inconsistent capitalization of the tags. I randomly picked "Motion Graphics" as the tag to search for, and after getting what seemed to be one less match that I should, I realized one video had the tag "motion graphics" (instead of "Motion Graphics"). I changed it to loop through the tags so that I could use strtolower(), which works but will be less efficient. This begs the question: could all this be done via the DB query, instead? (Assuming that's where the data is coming from?)

[code=php]
<pre><?php
$albums = unserialize(file_get_contents('albums.txt'));

$tag = 'Motion Graphics';
$result = array();

foreach($albums as $album) {
foreach($album['videos'] as $video) {
foreach($video['tags'] as $t) {
if(strtolower(trim($t)) == strtolower(trim($tag))) {
$result[] = $video;
break;
}
}
}
}

print_r($result);
?></pre>
[/code]
Copy linkTweet thisAlerts:
@cybercampbellauthorAug 13.2011 — Awesome..all working thanks loads!!

Unfortuanatly the data is coming from a third party api so I can only get it as a serialized php array, json or xml. I figured php was the easiest for me as that is what I'm using anyway. No access to the database.

Thanks again.

C
Copy linkTweet thisAlerts:
@cybercampbellauthorSep 02.2011 — Hi NogDog

I need a little help with a further customization of this script. The search for 'Tag' is perfect but I need another function that searches through the:

[title]

[album]

[description]

[tags]

This function will need to search through the string and match ANY word or phrase.

Also... the [description] is base64_encoded so will need to be decoded to search for the search term.

Any help will be very much appreciated.

I did try to modify your script but had no luck.

Cheers

C
Copy linkTweet thisAlerts:
@NogDogSep 03.2011 — Untested:
[code=php]
<?php
function searchArrayByTag($arr, $tag)
{
$result = array();
foreach($arr as $album) {
foreach($album['videos'] as $video) {
if (
stripos($video['title'], $tag) !== false or
stripos($video['description'], $tag) !== false or
stripos($video['album'], $tag) !== false
) {
$result[] = $video;
} else {
foreach($video['tags'] as $t) {
if (strtolower(trim($t)) == strtolower(trim($tag))) {
$result[] = $video;
break;
}
}
}
}
}
}
[/code]
Copy linkTweet thisAlerts:
@cybercampbellauthorSep 03.2011 — Worked perfectly!!!!

Thanks again.
×

Success!

Help @cybercampbell spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...