Jump to content

Add to array if value doesnt exist


doddsey_65

Recommended Posts

I have an array like so

 

Array (14)
(
|    ['0'] => Array (5)
|    (
|    |    ['name'] = String(7) "Pikemen"
|    |    ['hp'] = Integer(1) 0
|    |    ['atk'] = Integer(1) 5
|    |    ['def'] = Integer(1) 5
|    |    ['rng'] = Integer(2) 15
|    )
|    ['1'] => Array (5)
|    (
|    |    ['name'] = String(7) "Pikemen"
|    |    ['hp'] = Integer(1) 0
|    |    ['atk'] = Integer(1) 5
|    |    ['def'] = Integer(1) 5
|    |    ['rng'] = Integer(2) 15
|    )
|    ['2'] => Array (5)
|    (
|    |    ['name'] = String(7) "Pikemen"
|    |    ['hp'] = Integer(1) 0
|    |    ['atk'] = Integer(1) 5
|    |    ['def'] = Integer(1) 5
|    |    ['rng'] = Integer(2) 15
|    )
|    ['3'] => Array (5)
|    (
|    |    ['name'] = String(7) "Pikemen"
|    |    ['hp'] = Integer(1) 0
|    |    ['atk'] = Integer(1) 5
|    |    ['def'] = Integer(1) 5
|    |    ['rng'] = Integer(2) 15
|    )
|    ['4'] => Array (5)
|    (
|    |    ['name'] = String(7) "Pikemen"
|    |    ['hp'] = Integer(1) 0
|    |    ['atk'] = Integer(1) 5
|    |    ['def'] = Integer(1) 5
|    |    ['rng'] = Integer(2) 15
|    )
|    ['5'] => Array (5)
|    (
|    |    ['name'] = String(7) "Pikemen"
|    |    ['hp'] = Integer(1) 0
|    |    ['atk'] = Integer(1) 5
|    |    ['def'] = Integer(1) 5
|    |    ['rng'] = Integer(2) 15
|    )
|    ['6'] => Array (5)
|    (
|    |    ['name'] = String(7) "Pikemen"
|    |    ['hp'] = Integer(1) 0
|    |    ['atk'] = Integer(1) 5
|    |    ['def'] = Integer(1) 5
|    |    ['rng'] = Integer(2) 15
|    )
|    ['7'] => Array (5)
|    (
|    |    ['name'] = String(7) "Pikemen"
|    |    ['hp'] = Integer(1) 0
|    |    ['atk'] = Integer(1) 5
|    |    ['def'] = Integer(1) 5
|    |    ['rng'] = Integer(2) 15
|    )
|    ['8'] => Array (5)
|    (
|    |    ['name'] = String(7) "Pikemen"
|    |    ['hp'] = Integer(1) 0
|    |    ['atk'] = Integer(1) 5
|    |    ['def'] = Integer(1) 5
|    |    ['rng'] = Integer(2) 15
|    )
|    ['9'] => Array (5)
|    (
|    |    ['name'] = String(7) "Pikemen"
|    |    ['hp'] = Integer(1) 0
|    |    ['atk'] = Integer(1) 5
|    |    ['def'] = Integer(1) 5
|    |    ['rng'] = Integer(2) 15
|    )
|    ['10'] => Array (5)
|    (
|    |    ['name'] = String(7) "Pikemen"
|    |    ['hp'] = Integer(1) 0
|    |    ['atk'] = Integer(1) 5
|    |    ['def'] = Integer(1) 5
|    |    ['rng'] = Integer(2) 15
|    )
|    ['11'] => Array (5)
|    (
|    |    ['name'] = String(7) "Pikemen"
|    |    ['hp'] = Integer(1) 0
|    |    ['atk'] = Integer(1) 5
|    |    ['def'] = Integer(1) 5
|    |    ['rng'] = Integer(2) 15
|    )
|    ['12'] => Array (5)
|    (
|    |    ['name'] = String(7) "Pikemen"
|    |    ['hp'] = Integer(1) 0
|    |    ['atk'] = Integer(1) 5
|    |    ['def'] = Integer(1) 5
|    |    ['rng'] = Integer(2) 15
|    )
|    ['13'] => Array (5)
|    (
|    |    ['name'] = String(7) "Pikemen"
|    |    ['hp'] = Integer(1) 0
|    |    ['atk'] = Integer(1) 5
|    |    ['def'] = Integer(1) 5
|    |    ['rng'] = Integer(2) 15
|    )
)

 

what i want to do is add the details to a new multi dimensional array but only add one each like so

 

array(

    ['units'] = array(

          ['name'] => 'Pikemen',

          ['lost']    => 14

    )

)

basically where the HP is 0 in the first array i need to add them to the second if the name is unique. I have tried array unique but it didnt seem to work. Any ideas? All i get it a loop of each Pikemen

 

Link to comment
Share on other sites

 

basically where the HP is 0 in the first array i need to add them to the second if the name is unique. I have tried array unique but it didnt seem to work. Any ideas? All i get it a loop of each Pikemen

 

 

What is 'them'.

 

Do you mean if the 1st  is pikemen, and the second name is not pikemen, add some of the hp values together?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.