0

I want to add parent array key to child array.

Following is my main array.

Array
(
    [296] => Array
        (
            [0] => 02/22/2020
            [1] => 03/03/2020
        )

    [297] => Array
        (
            [0] => 02/22/2020
        )

    [300] => Array
        (
            [0] => 02/21/2020
        )

)

Code:

<?php
$dateARRa=array( array( "user_id"=>'296',
                'tour_date'=>array('dubai'=>'02/22/2020', 'Melbourne'=>'03/03/2020') ),
                array ('user_id'=>'297',
                'tour_date'=>array('Singapore'=>'02/22/2020') ),
                array( 'user_id'=>'300','tour_date'=>array('Sydeny'=>'02/21/2020') ),
                );

/*array( 'user_id'=>'303')*/
foreach ($dateARRa as $key => $value) {   
    $tourDates[$value['user_id']] =array_values($value['tour_date']);

}


echo "<pre>";print_r($tourDates);

$singleArray = []; 
foreach ($tourDates as $k=> $childArray) 
{ 
    //echo $k;
    foreach ($childArray as $ky=> $value) 
    {
        $singleArray[] = $value; 
    } 
}
echo "<pre>";print_r($singleArray);

When i print $singleArray following output is display.

Array
(
    [0] => 02/22/2020
    [1] => 03/03/2020
    [2] => 02/22/2020
    [3] => 02/21/2020
)

I have also tried like $singleArray[$k] = $value; in above childArray loop but it not work proper.

So I want to add main array key to this array so I want output like following array.

Array
(
    [296] => 02/22/2020
    [296] => 03/03/2020
    [297] => 02/22/2020
    [300] => 02/21/2020
)
7
  • 3
    Your proposed output is impossible, PHP arrays cannot have 2 identical keys. You would do better just to work from the $tourDates array. Commented Mar 24, 2020 at 5:19
  • @Nick, You are right but how can i manage that 296 users have 2 dates So i want each date by users , Do you have any another way? Commented Mar 24, 2020 at 5:21
  • 1
    The way you've done it in $tourDates is the other way. Since 296 has 2 dates, the value is an array with 2 dates in it. Commented Mar 24, 2020 at 5:22
  • @Barmar, Yes but I also want to display each date in single label Commented Mar 24, 2020 at 5:23
  • What does the array arrangement have to do with how you display it? Commented Mar 24, 2020 at 5:28

2 Answers 2

1

What you are trying to do is impossible but there are ways to achieve something similar.

Key-Value Pairs

You can not have two same indices in one array, but you can have an array of key-value pairs, if you change the line

$singleArray[] = $value;

with

$singleArray[] = ["key" => $k, "value" => $value];

you'll get this output:

Array
(
    [0] => Array
        (
            [key] => 296
            [value] => 02/22/2020
        )

    [1] => Array
        (
            [key] => 296
            [value] => 03/03/2020
        )

    [2] => Array
        (
            [key] => 297
            [value] => 02/22/2020
        )

    [3] => Array
        (
            [key] => 300
            [value] => 02/21/2020
        )

)

Key-Value Pairs (Object Oriented)

A better way is to use a specific data structure for this key-value pairs, for example use

class KeyValue {
    public $key;
    public $value;
    function __construct ($key, $value) {
        $this->key = $key;
        $this->value = $value;
    }
}

and then use

$singleArray[] = new KeyValue($k, $value); 

Or even more sophisticated, you can use

class UserDate {
    public $user;
    public $date;
    function __construct ($user, $date) {
        $this->user = $user;
        $this->date = $date;
    }
}

Array of Arrays

Which you done it already in $tourDates. For displaying this data you're gonna need two nested loops as you used to convert it to $singleArray.

foreach ($tourDates as $k=> $childArray) 
{ 
    //echo $k;
    foreach ($childArray as $ky=> $value) 
    {
        echo $k."\t".$value."\r\n"; 
    } 
}
Sign up to request clarification or add additional context in comments.

Comments

0

Instead of looping over $tourDates, maybe loop over $dateARRa

<?php
$dateARRa = array( 
    array( 
        'user_id'   => '296',
        'tour_date' => array( 
           'dubai'     => '02/22/2020', 
           'Melbourne' => '03/03/2020'
        ) 
    ),
    array ( 
        'user_id'   => '297',
        'tour_date' => array(
            'Singapore' => '02/22/2020'
        )
    ),
    array( 
        'user_id'   => '300',
        'tour_date' => array(
            'Sydeny' => '02/21/2020'
        )
    ),
);


$singleArray = []; 

// first we loop over $dateARRa
foreach ( $dateARRa as $k ) { 

    // we check that the variables we need exist
    if ( !empty( $dateARRa[$k]['user_id'] ) && ! empty( $dateARRa[$k]['tour_date'] ) ) ) {

        // Then, because we want to loop over an internal array, we begin that here. 
        // This is inefficient and should be avoided. But perfection is the enemy of good-enough.
        foreach( $dateARRa[$k]['tour_date'] as $tour_date ) {

            // we check if the user_id index exists on the `$singleArray`
            if ( ! empty( $singleArray[ $dateARRa[$k]['user_id'] ] ) ) {

                /* if it exists we can add a new tour-date to the array
                   note that here $tour_date would be equal to 'dubai', 'Melbourne', 'Singapore', or 'Sydeny', but $dateARRa[$k]['tour_date'][ $tour_date ] would be the date-value regardless of the name of the index (which is in this case the location-name)
                 */
                $singleArray[ $dateARRa[$k]['user_id'] ][] = $dateARRa[$k]['tour_date'][ $tour_date ];

                /* an alternative could be to push a new array-item containing both the location name and the date, but i'll leave that commented out because it's tangental
                   $singleArray[ $dateARRa[$k]['user_id'] ][] = [ 'location' => $tour_date, 'date' => $dateARRa[$k]['tour_date'][ $tour_date ]; 
                 */

            } else {

                /* and if it doesn't exist we create the index, using the user-id and an array with a single-index
                 */
                $singleArray[ $dateARRa[$k]['user_id'] ] = array( $tour_date );
            }

        }
    }
    $singleArray[ $dateARRa[ $k ][ 'user_id' ] ] = $dateARRa[ $k ][ 'user_id' ];
}
echo "<pre>";print_r($singleArray);

// expected output
Array
(
    [296] => [ 02/22/2020, 03/03/2020 ]
    [297] => [ 02/22/2020 ]
    [300] => [ 02/21/2020 ]
)

expected output

Array
(
    [296] => [ 02/22/2020, 03/03/2020 ]
    [297] => [ 02/22/2020 ]
    [300] => [ 02/21/2020 ]
)

Comments

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.