Jump to content

[SOLVED] Array combine HOW?


kucing

Recommended Posts

Dear friends and fellows,

 

I am stuck with this array combine stuff which I have tried all my skils but still no hope so I would appreciate any help if someone could :)

 

Ok here is what I am facing.

 

$array=array (
  'UserIds' => 
  array (
    0 => '32',
    1 => '33',
    2 => '34',
  ),
  'UserAge' => 
  array (
    0 => '14',
    1 => '27',
    2 => '32',
  ),
  'Income' => 
  array (
    0 => '1100',
    1 => '900',
    2 => '1350',
  ),
  'Status' => 
  array (
    0 => '01',
    1 => '00',
    2 => '00',
  ),
);

 

 

and when I do print_r($array);

It show me result like this

 

Array
(
    [userIds] => Array
        (
            [0] => 32
            [1] => 33
            [2] => 34
        )

    [userAge] => Array
        (
            [0] => 14
            [1] => 27
            [2] => 32
        )

    [income] => Array
        (
            [0] => 1100
            [1] => 900
            [2] => 1350
        )

    [status] => Array
        (
            [0] => 01
            [1] => 00
            [2] => 00
        )

)

 

Also about the username, userage, income and status could be random as users also allowed to make new things which stores in one array.

 

And my problem start right here

After wasting my 2 days I have no choice but to ask help :(

I want result to be like this

 

Array
(
     [0] => 32
     [1] => 33
     [2] => 34
     [3] => 14
     [4] => 27
     [5] => 32
     [6] => 1100
     [7] => 900
     [8] => 1350
     [9] => 01
     [10] => 00
     [11] => 00
)

 

Here i do not want to display any username, userage, income or Status but want all the subarrays in one array..

Thank you for you help I really appreciate it :)

 

 

K

Link to comment
Share on other sites

sorry:(

 

my bad i wrote that by mistake actually i want it like this

 

Array
(
     [0] => 32
     [1] => 33
     [2] => 34
     [3] => 14
     [4] => 27
     [5] => 32
     [6] => 1100
     [7] => 900
     [8] => 1350
     [9] => 01
     [10] => 00
     [11] => 00
)

 

Also about the username, userage, income and status could be random as users also allowed to make new things which stores in one array.

 

I really appreciate your kind help :)

Thanks

 

 

Link to comment
Share on other sites

Actually that was just an example to make this post easy to understand and in real every user has its own array which is using the same way of the array shown above.

 

Why I want it to be like that because I want to use array_slice

 

Example:

$arraybreak = array_slice($array, 0, 1);

 

And when I do that in this $array

It give me result like this which I don't want :(

 

Array
(
    [userIds] => Array
        (
            [0] => 32
        )

    [userAge] => Array
        (
            [0] => 14
        )

    [income] => Array
        (
            [0] => 1100
        )

    [status] => Array
        (
            [0] => 01
        )
)

 

But I only want it to show me

 

Array
(
    [userIds] => Array
        (
            [0] => 32
        )
)

 

I am totally confuse..

 

Thanks

Link to comment
Share on other sites

I'm not entirely sure I grasp what you're trying to do, but if I'm reading you correctly you might try using foreach to go through the array, specifing which keys you want to extract your data from:

 

foreach($array as $key=>$value)
{
     if($key == 'UserIds')
     {
          foreach($array[$key] as $userid)
          {
               //do what you are going to do with the var $userid
          }
     }
}

 

Good luck!

 

Trev

Link to comment
Share on other sites

I'm not entirely sure I grasp what you're trying to do, but if I'm reading you correctly you might try using foreach to go through the array, specifing which keys you want to extract your data from:

 

foreach($array as $key=>$value)
{
     if($key == 'UserIds')
     {
          foreach($array[$key] as $userid)
          {
               //do what you are going to do with the var $userid
          }
     }
}

 

Good luck!

 

Trev

 

I appreciate your help thanks but as I was being doing that before this problem started only after I added a feature for my users to make their own questions and when it comes to user define array keys my foreach doesnt work there since they making their own questions and answers and I got no clue what the question would be.

 

An example:

 

Array
(
    [abcdxyz] => Array
        (
            [0] => xyz
        )
)

 

 

so that loop was good if i had default values..

 

 

Array
(
    [userID] => Array
        (
            [0] => 200
        )
)

 

So I was looking for a way to make those subarrays combine with all the arrays if there is a way I would really love to know

 

Thanks in advance

Link to comment
Share on other sites

I wanted to combine all the subarrays in one array and I solved it using tllewellyn given codes

and added $myTestArray which now contain all the subarrays data

 

foreach($array as $key=>$value)
{
          foreach($array[$key] as $userid)
          {
               //do what you are going to do with the var $userid
$myTestArray["test"][]=$userid;
     }
}

 

Thanks :)

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.