Jjames
Jjames

Reputation: 183

using unserialized php array to parse image array

This is probably an easy question for some, but not for me unfortunately.. I am retrieving a serialized array from a MySql DB. That works fine. It unserializes fine. I am then retrieving an array of images I need to display in the order of the unserialized array:

$order = unserialize( $row['displayOrder']);

this will result in:

Array ( [0] => 2296543905 [1] => 2296540032 [2] => 2296540820 [3] => 2296541625 [4] => 2296541997 [5] => 2296542417 [6] => 2296542816 [7] => 2296543113 [8] => 2296545096 [9] => 2296544278

I am retrieving another array of image URLs, like so:

$images = $images['Images'];
foreach ( $images as $index => $image) {
echo '<li><img src="'.$image['TinyURL'].'" class="framed"></li>';
}

Which results in:

   "Images": [
  {
    "id": 2296543905,
    "TinyURL": "http://blah.com"
  }
]

"Images": [
  {
    "id": 2296543905,
    "TinyURL": "http://blah.com"
  }
]

My question is...how would I parse through to get the imagesTinyURL to display in the order of the unserialized array? i.e: list the image urls in order of the order array by the id in the Images array?

Upvotes: 1

Views: 336

Answers (1)

Dracony
Dracony

Reputation: 842

Here is a small example:

//Create an array where the keys are image ids
$imageArray=array();
foreach($images as $image)
    $imageArray[$image['id']]=$image;

//Output the images according to the order array (array of image ids)
foreach($order as $id){
    print_r($imageArray[$id]);
}

Upvotes: 1

Related Questions