2

I'm having a terrible time trying to pull out some values out of an array. Here is a slimmed down version of the array.

$properties = array( array( PropertyID => 2845, 
                  Address_1 => "1234 Any street",
                  MEDIA_IMAGE_00 => "23428.jpg",
                  MEDIA_IMAGE_TEXT_00 => "Front of House",
                  MEDIA_IMAGE_01 => "29872.jpg",
                  MEDIA_IMAGE_TEXT_01 => "Master Bedroom",
                  MEDIA_IMAGE_02 => "29834.jpg"
                ),
              array( PropertyID => 2845, 
                  Address_1 => "555 This street",
                  MEDIA_IMAGE_00 => "234234.jpg",
                  MEDIA_IMAGE_TEXT_00 => "Front of House",
                  MEDIA_IMAGE_01 => "298724.jpg",
                  MEDIA_IMAGE_TEXT_01 => "Second Bedroom",
                  MEDIA_IMAGE_02 => "298346.jpg"
                ),
              array( PropertyID => 2845, 
                  Address_1 => "333 Main street",
                  MEDIA_IMAGE_00 => "2342845.jpg",
                  MEDIA_IMAGE_TEXT_00 => "Lounge",
                  MEDIA_IMAGE_01 => "2987246.jpg",
                  MEDIA_IMAGE_TEXT_01 => "Front of House",
                  MEDIA_IMAGE_02 => "2983434.jpg"
                ),
         );

There is a massive amount of data in each sub array I've trimmed it down for length...

I'm inserting this data into a MySQL database, however, I'm inserting the images into a separate table [PropertyID, Image, ImageText] because some properties may have more images than others.

So now that the background is out of the way.

How do I pull just the keys of the array that match and there info into another array? So that I would end up with an array from the above that would end up with something similar to:

$property_images = array( array( PropertyID => 2845, 
                         IMAGE => "23428.jpg",
                         IMAGE_TEXT => "Front of House"),
                  array( PropertyID => 2845,
                         IMAGE => "29872.jpg",
                         IMAGE_TEXT => "Master Bedroom",
                  array( PropertyID => 2845,
                         MEDIA_IMAGE_02 => "29834.jpg"
                         IMAGE_TEXT => "Living Room"

I've tried sscanf to no avail and fiddle around with array_keys but haven't managed to figure out how to target the key names rather than the key values...

Thanks for your help in advance!

1 Answer 1

2

Edit:

foreach( $properties as $v ) {
 foreach( $v as $k => $m ) {     

  if( strpos($k, 'MEDIA_IMAGE_TEXT_') !== FALSE  ) {        
    $new_array[] = array('PropertyID' => $v['PropertyID'], 'IMAGE'=>$prev, 'IMAGE_TEXT'=>$m );
  } 

  if( strpos($k, 'MEDIA_IMAGE_') !== FALSE ) {
    $prev = $m;  //var_dump($prev);
  }

 }
}
Sign up to request clarification or add additional context in comments.

8 Comments

That's a great start! But I need to iterate over each MEDIA_IMAGE_XX and MEDIA_IMAGE_TEXT_XX.
I need something like for each array_key name that matches "MEDIA_IMAGE_" + (twodigits) grab that array key's value and put it in the new array.
the 1st foreach will iterate over each MEDIA_IMAGE_00 and MEDIA_IMAGE_TEXT_00. Note: MEDIA_IMAGE_TEXT_XX not equal to MEDIA_IMAGE_TEXT_00.
oh ok, as you mentioned _00 in your question, I mentioned that. I will edit my answer, give me a minute.
Right... But I need it to grab out MEDIA_IMAGE_00, MEDIA_IMAGE_01, MEDIA_IMAGE_02, etc... As well as, MEDIA_IMAGE_TEXT_00, MEDIA_IMAGE_TEXT_01, MEDIA_IMAGE_TEXT_02... Then move onto the next sub-array and do it all over again for as many MEDIA_IMAGE_** and MEDIA_IMAGE_TEXT_** that are in each the sub-array
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.