Array combine

Discussion in 'PHP' started by Fracisc, Mar 20, 2014.

  1. #1
    I have this:
    foreach (array_combine($img, $name) as $img => $title) {
    echo $img;
    echo $title;
    } 
    PHP:
    The problem is that array_combine will remove duplicates. So, if I have two ore more entries like this
    image1 name1
    image1 name1

    It will only show them once. I need to be able to print them on each occurrence.

    How can I do that?

    Thanks!
     
    Fracisc, Mar 20, 2014 IP
  2. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #2
    Try array_merge ....

    stay well
     
    Vooler, Mar 20, 2014 IP
  3. Fracisc

    Fracisc Well-Known Member

    Messages:
    3,670
    Likes Received:
    10
    Best Answers:
    1
    Trophy Points:
    195
    #3
    That doesn't see to do that I want.. Now if I echo $title all the titles are in one line.. Like title1title1title1
     
    Fracisc, Mar 20, 2014 IP
  4. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #4
    You question is also a but confusing. Kindly post example of input and desired out and we can advise :)

    thanks
     
    Vooler, Mar 20, 2014 IP
  5. Fracisc

    Fracisc Well-Known Member

    Messages:
    3,670
    Likes Received:
    10
    Best Answers:
    1
    Trophy Points:
    195
    #5
    I have two textarea fields in a form. One is images and the 2nd is names.
    I need to echo the output like this:
    echo $image; echo $name;

    The problem occurs if I have this in my textareas

    In names:
    test
    test
    test

    In images:
    img1
    img1
    img1

    The array_merge works great if I have no duplicates. In the case presented above, my echo is this:
    test img1

    it should be
    test img1
    test img1
    test img1

    Was I clear enough this time? :) I'm not very good in explaining and my English is not so good either..

    [EDIT]

    Sorry, I meant array_combine works great if..
     
    Fracisc, Mar 20, 2014 IP
  6. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #6
    Your English is very fine :) And problem is because array can hold one unique key not multiple of same name ..

    foreach ($images  as $key => $img)
    {
        $title = $titles[$key]; //note this
        echo $img;
        echo $title;
    } 
    PHP:
     
    Vooler, Mar 20, 2014 IP
  7. Fracisc

    Fracisc Well-Known Member

    Messages:
    3,670
    Likes Received:
    10
    Best Answers:
    1
    Trophy Points:
    195
    #7
    The suggested code returns this:

    img1testimg1eimg1

    It should be
    img1 test
    img1 test
    img1 test
     
    Fracisc, Mar 20, 2014 IP
  8. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #8
    instead of
    echo $img;
    echo $title;

    use

    echo "$img $title\n";
     
    Vooler, Mar 20, 2014 IP
  9. Fracisc

    Fracisc Well-Known Member

    Messages:
    3,670
    Likes Received:
    10
    Best Answers:
    1
    Trophy Points:
    195
    #9
    The result is the same. Here it is: img1 test img1 e img1
     
    Fracisc, Mar 20, 2014 IP
  10. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #10
    \n doesn't show in browser, use <br>
    echo "$img $title<br>";
     
    Vooler, Mar 20, 2014 IP
  11. Fracisc

    Fracisc Well-Known Member

    Messages:
    3,670
    Likes Received:
    10
    Best Answers:
    1
    Trophy Points:
    195
    #11
    That's the same...

    Anyways, thanks for your help and patience. I have found a solution which works.
     
    Fracisc, Mar 20, 2014 IP
  12. salmanshafiq

    salmanshafiq Well-Known Member

    Messages:
    260
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    128
    #12
    Can you please write down the solution? So that will be helpful for me ... I am doing this with alot of code....
     
    salmanshafiq, Mar 20, 2014 IP
  13. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #13
    I'm just stuck trying to figure out what in the blazes you're trying to accomplish with that... nothing in this thread makes the least bit of sense to even be doing in the first place.
     
    deathshadow, Mar 20, 2014 IP
  14. Fracisc

    Fracisc Well-Known Member

    Messages:
    3,670
    Likes Received:
    10
    Best Answers:
    1
    Trophy Points:
    195
    #14
    deathshadow: I have images with numbers as name (123.jpg) and I want to bulk rename them to name.jpg
     
    Fracisc, Mar 20, 2014 IP
  15. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #15
    And... How does that make sense to have several files named the same? This is anyway why we have ways to manipulate a filesystem...
     
    PoPSiCLe, Mar 20, 2014 IP
  16. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #16
    Yes, but WHAT name?!? Do you have a list of names to replace them with or are you just blindly going to rename them all the same (which is what it sounds like)... Doesn't make any sense. Sure as shine-ola doesn't sound like anything that can be automated unless your intent is to change their name to their title, and that's still a loaded gun to your head given the probability of collisions. (duplicate names).
     
    deathshadow, Mar 20, 2014 IP
  17. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #17
    You should post the solution too, here is mine in the event someone else comes to this thread:

    
    function combine_custom($key, $val){
        return array('img' => $key, 'title' =>$val);
    }
    $results = array_map('combine_custom', $img, $name);
    foreach ($results as $row) {
        echo $row['img'];
        echo $row['title'];
    }
    
    PHP:
     
    ThePHPMaster, Mar 20, 2014 IP