0

I just found out how to loop an array based on another array, but my problem is that if the second array is just 1 object than it works fine but I want to make it work with two objects.

Here is an example how it do works,

$shorten = array(
  0 => 'ECAR',
  1 => 'CCAR',
  2 => 'ICAR',
  3 => 'SCAR',
  4 => 'FCAR',
  5 => 'PCAR',
);

$data = array(
  'Hertz' => array(
        'ECAR' => '49.21',
        'CCAR' => '71.04',
        'ICAR' => '89.58',
        'SCAR' => '100.00',
    )
  ),
  'Avis' => array(
        'ECAR' => '412.00',
        'CCAR' => '347.00',
        'ICAR' => '285.00',
        'SCAR' => '224.00',
        'FCAR' => '165.00',
        'PCAR' => '100.00',
    )
  ),
);

// default array as the base
$shorten = array_combine($shorten, array_fill(0, count($shorten), 'n/a'));

foreach($data as &$array) {
    // merge to get set members
    $array = array_merge($shorten, $array);
}
unset($array);

print_r($data);

But I want to make it work with this one,

$shorten = array(
  0 => 'ECAR',
  1 => 'CCAR',
  2 => 'ICAR',
  3 => 'SCAR',
  4 => 'FCAR',
  5 => 'PCAR',
);

$data = array(
  'Hertz' => array(
    'NYCT01' => array(
        'ECAR' => '49.21',
        'CCAR' => '71.04',
        'ICAR' => '89.58',
        'SCAR' => '100.00',
    )
  ),
  'Avis' => array(
    'NYCT01' => array(
        'ECAR' => '412.00',
        'CCAR' => '347.00',
        'ICAR' => '285.00',
        'SCAR' => '224.00',
        'FCAR' => '165.00',
        'PCAR' => '100.00',
    )
  ),
);

// default array as the base
$shorten = array_combine($shorten, array_fill(0, count($shorten), 'n/a'));

foreach($data as $firstArray) {
    foreach($firstArray as &$array){
        // merge to get set members
        $array = array_merge($shorten, $array);
    }
}
unset($array);

print_r($data);

And this is what I want it to be,

Array
(
    [Hertz] => Array
        (
            [ECAR] => 49.21
            [CCAR] => 71.04
            [ICAR] => 89.58
            [SCAR] => 100.00
            [FCAR] => n/a
            [PCAR] => n/a
        )

    [Avis] => Array
        (
            [ECAR] => 412.00
            [CCAR] => 347.00
            [ICAR] => 285.00
            [SCAR] => 224.00
            [FCAR] => 165.00
            [PCAR] => 100.00
        )

)
2
  • $data[1][0][0] = 412.00; this is how your would access array in array in array etc. if thats what you meant, is it? I do not really understand your question. Commented Jun 13, 2012 at 14:01
  • Please check the array what I want it should be, thanx Commented Jun 13, 2012 at 14:07

2 Answers 2

1
foreach($data AS $company => $nyc){
    $inner = $nyc['NYCT01'];
    foreach($shorten AS $car){
        if(array_key_exists($car, $inner)){
            $output[$company][$car] = $inner[$car];
        }else {
            $output[$company][$car] = "n/a";
        }
    }
}

Just loop through the data and check if the key of any of the cars exists inside the NYCT01 array, if it does set the value, else, "n/a".

My output:

Array
(
    [Hertz] => Array
        (
            [ECAR] => 49.21
            [CCAR] => 71.04
            [ICAR] => 89.58
            [SCAR] => 100.00
            [FCAR] => n/a
            [PCAR] => n/a
        )

    [Avis] => Array
        (
            [ECAR] => 412.00
            [CCAR] => 347.00
            [ICAR] => 285.00
            [SCAR] => 224.00
            [FCAR] => 165.00
            [PCAR] => 100.00
        )

)
Sign up to request clarification or add additional context in comments.

5 Comments

This is a good idea, but my problem is that the $inner is different in my array, I mean it might be for example NYCT02 or NYCC01 etc.
Then alternatively you could loop through $nyc
Yes you're right, I'm trying to do that, I'm struggling a little here, hope to figure it out.
Thank you Marcus I got it work, please see the answer I entered,
No problem! Mark the question as answered then.
0

Ok, got it work this is the real code I figured,

foreach($data as $company => $nyc){
    foreach($nyc as $inner => $s){
        foreach($shorten as $car){
            if(array_key_exists($car, $nyc[$inner])){
                $output[$company][$car] = $nyc[$inner][$car];
            }else {
                $output[$company][$car] = "n/a";
            }
        }
    }
}

Thanks so much for your help Marcus,

Good luck.

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.