0

This is my simple looper code

foreach( $cloud as $item ) {

    if ($item['tagname'] == 'nicetag') {
        echo $item['tagname'];
            foreach( $cloud as $item ) {
                echo $item['desc'].'-'.$item['date'];
            }
    } else 
        //...
    }

I need to use if method in this looper to get tags with same names but diferent descriptions and dates. The problem is that I dont know every tag name becouse any user is allowed to create this tags. Im not really php developer so I'm sory if it's to dummies question and thanks for any answers!

Dimitri Dewaele
  • 10,311
  • 21
  • 80
  • 127
wpanin
  • 25
  • 1
  • 9
  • Could you explain a little bit more about what you want to do? – Paul Oostenrijk Dec 21 '14 at 10:44
  • Hello Paul. I need to find all [tagname] values in multidimensional array and if value is duplicate, loop there [desc] and [date] values. In output it must be somethink like this: - first tag -- nice description 12 - 02-02-2014 -- nice description 52 - 05-02-2014 -- nice description 13 - 07-02-2014 - second tag -- nice description 11 - 03-02-2014 -- nice description 51 - 04-02-2014 Maybe I need to rebuild an array? – wpanin Dec 21 '14 at 11:00

1 Answers1

1

One possible solution is to declare a temporary variable that will hold tagname that is currently looped through:

$currentTagName = '';
foreach( $cloud as $item ) {
    if ($item['tagname'] != $currentTagName) {
        echo $item['tagname'];
        $currentTagName = $item['tagname'];
    }

    echo $item['desc'] . '-' . $item['date'];
}

I presume that your array structure is as follows:

$cloud array(
    array('tagname' => 'tag', 'desc' => 'the_desc', 'date' => 'the_date'),
    array('tagname' => 'tag', 'desc' => 'the_desc_2', 'date' => 'the_date_2'),
    ...
);

BUT

This solution raises a problem - if your array is not sorted by a tagname, you might get duplicate tagnames.

So the better solution would be to redefine your array structure like this:

$cloud array(
    'tagname' => array (
        array('desc' => 'the_desc', 'date' => 'the_date'),
        array('desc' => 'the_desc_2', 'date' => 'the_date_2')
    ),
    'another_tagname' => array (
        array('desc' => 'the_desc_3', 'date' => 'the_date_3'),
        ...
    )
);

and then you can get the data like this:

foreach ($cloud as $tagname => $items) {
    echo $tagname;

    foreach($items as $item) {
        echo $item['desc'] . '-' . $item['date'];
    }
}
  • Thank you!!! This is it :) May I ask you how to redefine this array? It will be really great – wpanin Dec 21 '14 at 11:56
  • You need to rewrite the function/method where this array is built, because restructuring the current array is not very efficient. – Vytautas Lozickas Dec 21 '14 at 12:20
  • Thanks for your answer! The problem is that I get array using WP_Query. I dont really belive that I can to rewrite this function by myself... so maybe I just sort array by a tagname... – wpanin Dec 21 '14 at 12:59
  • Maybe you can try altering the query itself (the parameters sent to WP_Query) to get the array you need? Or at least add an _order by_ clause so you don't need to sort the array manually. I don't have much experience with WordPress though... – Vytautas Lozickas Dec 21 '14 at 13:14