0

all

In my aap, I need to short images based on Tag. Now in first array there is image tag and in second is url.

  1. $results_tag array

    Array
    (
        [0] => stdClass Object
        (
            [meta_value] => Tag 2
        )        
    )
    Array
    (
        [0] => stdClass Object
        (
            [meta_value] => Tag 1
        )
    
        [1] => stdClass Object
        (
            [meta_value] => Tag 3
        )  
    )
    
  2. $results_url array

     Array
        (
            [0] => stdClass Object
                (
                    [meta_value] => formidable/madness_beach.jpg
                )
    
        )
        Array
        (
            [0] => stdClass Object
            (
                [meta_value] => formidable/adfish_beachchair1.jpg
            )
    
            [1] => stdClass Object
            (
                [meta_value] => formidable/madness_boat1.jpg
            )
    )
    

Now, what i actually want is

Array
(
[Tag 2] => [formidable/madness_beach.jpg]
[Tag 1] => [formidable/adfish_beachchair1.jpg]
[Tag 3] => [formidable/madness_boat1.jpg]
)

So , using this i can short image based on tag.

I have tried

$results_tag = array_merge_recursive($results_tag,$results_url);

But not getting the output as i want.

Thanks In Advance!!

3
  • $results_tag seems got 2 array of objects Commented Feb 22, 2012 at 10:29
  • yes but both array have same object & value Commented Feb 22, 2012 at 10:30
  • $results_tag contains an array. Each element of the array is an array stdClass object. <- Read it carefully. It seems the variable has 1 more nested levels Commented Feb 22, 2012 at 10:36

5 Answers 5

3

I assume the number of elements is known to be $N.

$results = array();
for ( $i = 0; $i < $N; ++$i ) $results[$results_tag[$i]->meta_value] = $results_url[$i]->meta_value;
Sign up to request clarification or add additional context in comments.

2 Comments

Also make sure that no two tags are the same, since you will then lose data.
Yep! i know that is the issue so i asked this one. I found same solun like yours stackoverflow.com/questions/6725954/…
2

This will do.

$result = array();
$len = count($results_tag);
while($len--) $result[$results_tag[$i]->meta_value] = $results_url[$i]->meta_value;

Comments

2
 <?php 

  $results_tag_one =  array(0 => array('meta_value' => 'Tag 2'));
  $results_tag_two =  array(0 => array('meta_value' => 'Tag 1'),1 => array('meta_value' => 'Tag 3'));
  $results_tag = array_merge($results_tag_one,$results_tag_two);

  $results_url_one =  array(0 => array('meta_value' => 'formidable/madness_beach.jpg'));
  $results_url_two =  array(0 => array('meta_value' => 'formidable/adfish_beachchair1.jpg'),1 => array('meta_value' => 'formidable/madness_boat1.jpg'));
  $results_url = array_merge($results_url_one,$results_url_two);

  $final = array();
  $i = 0;
  foreach ($results_url as $url_data){
    $final[$results_tag[$i]['meta_value']] = $results_url[$i]['meta_value'];
    $i++;
  }
  echo "<pre>";
  print_r($final);
  echo "</pre>";
  exit;

cheers

Comments

1
function flatten($tags, $urls, &$result) {
  if (is_array($tags))
    while (sizeof($tags)>0) {
      $tagschld=array_shift($tags);
      $urlschld=array_shift($urls);
      flatten($tagschld, $urlschld, $result);
    }
  else $result[$tags->meta_value]=$urls->meta_value;
}

$result=array();
flatten($results_tag, $results_url, $result);
print_r($result);

Comments

1

As far as I know, there is no built-in function which transforms two array of objects into one associative array based on one property.

You could implement something like this assuming you have the correspondence between the array keys:

$results = array();
foreach($results_tag as $i => $item)
{
    if(is_array($item){
       foreach($item as $j => $value) {
          $results[$value->meta_value] = $results_url[$i][$j]->meta_value;
       }
    } else {
       $results[$value->meta_value] = $results_url[$i]->meta_value;
    }
}

1 Comment

I updated the code to handle both cases: elements in the array, or elements in subarrays. Hope this works

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.