Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I and trying to have an if else() statement look for 2 or more conditions before executing but its not working for me.. My ultimate goal for example is to search for array[Pack] and if [Type] == s45 && [packageA1] exist then to use array_slice to add additional info to the [Type array]

The if() condition works fine but not the else if()....

$node:

Array
(
    [Pack] => Array
        (
            [Type] => s45
            [packageA1] => Array
                (
                    [level] => REMOVE FROM DB
                    [stage] => REMOVE FROM DB
                    [description] => s45 info here
                    [image] => 
                )

        )

    [Pack2] => Array
        (
            [Type] => s99
            [packageA1] => Array
                (
                    [level] => REMOVE FROM DB
                    [stage] => REMOVE FROM DB
                    [description] => s99 info goes here
                    [image] => 
                )

        )
)

//more code...
$i = 1;
if(!array_key_exists($item[0], $node)){
     $node[$row[0]] = array("Type" => $item[0], "package".$item[1] => array("level" => "REMOVE FROM DB", "stage" => "REMOVE FROM DB", "description" => $item[3], "image" => $item[4]));
}else if(array_key_exists($item[0], $node) && array_key_exists("package".$item[1], $node)){
     $i++;
     $res = array_slice($node[$rowKey], 0, $i, true) +  array("my_key" => "my_value") + array_slice($node[$rowKey], $i, count($node[$rowKey]) - 1, true);
}

Expected Output:

Array
(
    [Pack] => Array
        (
            [Type] => s45
            [packageA1] => Array
                (
                    [level] => REMOVE FROM DB
                    [stage] => REMOVE FROM DB
                    [description] => s45 info here
                    [image] => 
                )
            [packageA2] => Array
                (
                    [level] => REMOVE FROM DB
                    [stage] => REMOVE FROM DB
                    [description] => blahhhhhh blah blahh
                    [image] => 
                )
            [packageE1] => Array
                (
                    [level] => REMOVE FROM DB
                    [stage] => REMOVE FROM DB
                    [description] => moar random stuff inserted
                    [image] => 
                )    
        )

    [Pack2] => Array
        (
            [Type] => s99
            [packageA1] => Array
                (
                    [level] => REMOVE FROM DB
                    [stage] => REMOVE FROM DB
                    [description] => s99 info goes here
                    [image] => 
                )

        )
)
share|improve this question
 
in_multi_array is not a default PHP function .... where is the code to that ? –  Baba Nov 8 '12 at 2:18
 
im sorry, i was testing another function..edited it –  user1784477 Nov 8 '12 at 2:19
 
And what do you mean by && [packageA1] ?? –  Baba Nov 8 '12 at 2:19
 
If the [Type] && the [packageA1] exist in the [Pack] array then ill use array_slice to add addition arrays into [Type] –  user1784477 Nov 8 '12 at 2:21
 
What additional info are you adding to Type array –  Baba Nov 8 '12 at 2:25
show 3 more comments

1 Answer

up vote 0 down vote accepted

You can use array_filter

$filter = array_filter($data, function ($p) {
    return $p['Type'] == "s45" && is_array($p['packageA1']);
});


$packageA2 = array(
        'level' => 'REMOVE FROM DB',
        'stage' => 'REMOVE FROM DB',
        'description' => 'blahhhhhh blah blahh',
        'image' => NULL);

$packageE1 = array(
        'level' => 'REMOVE FROM DB',
        'stage' => 'REMOVE FROM DB',
        'description' => 'moar random stuff inserted',
        'image' => NULL,
);

$filter['Pack']['packageA2'] = $packageA2 ;
$filter['Pack']['packageE1'] = $packageE1 ;
var_dump($filter);

Output

array
  'Pack' => 
    array
      'Type' => string 's45' (length=3)
      'packageA1' => 
        array
          'level' => string 'REMOVE FROM DB' (length=14)
          'stage' => string 'REMOVE FROM DB' (length=14)
          'description' => string 's45 info here' (length=13)
          'image' => null
      'packageA2' => 
        array
          'level' => string 'REMOVE FROM DB' (length=14)
          'stage' => string 'REMOVE FROM DB' (length=14)
          'description' => string 'blahhhhhh blah blahh' (length=20)
          'image' => null
      'packageE1' => 
        array
          'level' => string 'REMOVE FROM DB' (length=14)
          'stage' => string 'REMOVE FROM DB' (length=14)
          'description' => string 'moar random stuff inserted' (length=26)
          'image' => null
share|improve this answer
 
Your example works good on its own but im still confused on how to implement it into my code. How is array_filter replacing an if(){}else{} statement? –  user1784477 Nov 8 '12 at 4:48
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.