Problem with in_array ()

6

I have a problem with the in_array function.

I made a var_dump() in the array I want to search for:

array(2) { 
    ["28c8edde3d61a0411511d3b1866f0636"]=> array(8) { 
        ["id"]=> string(32) "c4ca4238a0b923820dcc509a6f75849b" 
        ["qty"]=> float(1) 
        ["price"]=> float(2500) 
        ["name"]=> string(13) "Album: Xim-ER" 
        ["type"]=> string(5) "Asset" 
        ["weight"]=> string(3) "115" 
        ["rowid"]=> string(32) "28c8edde3d61a0411511d3b1866f0636" 
        ["subtotal"]=> float(2500) 
    } 
    ["dc74f1aaf0e81b424c56cbd906f3d3c3"]=> array(8) { 
        ["id"]=> string(33) "Dc4ca4238a0b923820dcc509a6f75849b" 
        ["qty"]=> float(1) 
        ["price"]=> float(2000) 
        ["name"]=> string(21) "Album: Xim-ER Digital" 
        ["type"]=> string(7) "Digital" 
        ["weight"]=> string(1) "0" 
        ["rowid"]=> string(32) "dc74f1aaf0e81b424c56cbd906f3d3c3" 
        ["subtotal"]=> float(2000) 
    }
}

I need to see in this Array if I have Asset, so I did the following in my controller:

<?php

$order->Shipping = new stdClass ();
if (in_array ( array ('type','Asset'), $cart_array )) {
    $order->Shipping->Type = 'Correios';
    $order->Shipping->SourceZipCode = '25670202';
    $order->Shipping->TargetZipCode = $this->session->userdata ( 'user_cep' );
    $order->Shipping->Address = new stdClass ();
    $order->Shipping->Address->Street = $this->session->userdata ( 'user_adress' );
    $order->Shipping->Address->Number = $this->session->userdata ( 'user_adress_number' );
    $order->Shipping->Address->Complement = $this->session->userdata ( 'user_adress_comp' );
    $order->Shipping->Address->District = $this->session->userdata ( 'user_adress_district' );
    $order->Shipping->Address->City = $this->session->userdata ( 'user_city' );
    $order->Shipping->Address->State = $this->session->userdata ( 'user_state' );
} else {
    $order->Shipping->Type = 'WithoutShipping';
}

But it only returns shipping with WithoutShipping.

I forgot some parameter to in_array correctly?

    
asked by anonymous 11.11.2015 / 10:54

5 answers

4

I managed to find a way. In fact an in_array and an array_column are used to specify the array.

if(in_array('Asset', array_column($cart_array, 'type'))){}
    
11.11.2015 / 11:45
1
The problem in my point of view is that you are looking for an array inside another, so I understand you want to see inside the $ cart_arry array if there is an Asset object, but the in_array I understand will not help in_array

$myArray = array("Audi","BMW","Lexus","Mercedes");

var_dump($myArray);

if (in_array("Lexus", $myArray)) {
    echo "Lexus was found in the array<br/>";
}

if (in_array("opel", $myArray)) {
    echo "Opel was found in the array<br/>";
}

For this you may have to do otherwise, run this list and check the type.

I hope to have helped clarify your doubts.

    
11.11.2015 / 11:05
1

There is an easy way to search recursively for a value in a multidimensional array that works on all versions of PHP 5.

/**
 * @param $needle
 * @param $haystack
 * @param bool $strict
 *  Função in_array recursiva - Mantido mesma assinatura que in_array para facilitar uso
 */
function in_array_recursive($needle, $haystack, $strict = false) {
    $found = false;
    foreach ($haystack as $key => $value) {
        $found = $strict == false ? $needle == $value : $needle === $value;
        if ($found == false) {
            if (is_array($value)) {
                return in_array_recursive($needle, $value, $strict);
            }
        } else {
            break;
        }
    }
    return $found;
}

Here working in your code.

$cart_array = array(
    "28c8edde3d61a0411511d3b1866f0636" => array(
        "id" => "c4ca4238a0b923820dcc509a6f75849b",
        "qty" => (float)1,
        "price" => (float)2500,
        "name" => "Album: Xim-ER",
        "type" => "Asset",
        "weight" => "115",
        "rowid" => "28c8edde3d61a0411511d3b1866f0636",
        "subtotal" => (float)2500,
    ),
    "dc74f1aaf0e81b424c56cbd906f3d3c3" => array(
        "id" => "Dc4ca4238a0b923820dcc509a6f75849b",
        "qty" => (float)1,
        "price" => (float)2000,
        "name" => "Album: Xim-ER Digital",
        "type" => "Digital",
        "weight" => "0",
        "rowid" => "dc74f1aaf0e81b424c56cbd906f3d3c3",
        "subtotal" => (float)2000,
    )
);

if (in_array_recursive('Asset', $cart_array)) {
   echo 'WithShipping';
} else {
    echo 'WithoutShipping';
}
    
11.11.2015 / 19:45
0

You are checking a multidimensional array and in_array() is not recursive, ie it will not look for the value in the second level of the array.

Before doing the check, loop to iterate the array:

<?php

$order->Shipping = new stdClass ();

foreach ( $cart_array as $cart_item ) {
    if (in_array ( array('type','Asset'), $cart_item )) {
        $order->Shipping->Type = 'Correios';
        $order->Shipping->SourceZipCode = '25670202';
        $order->Shipping->TargetZipCode = $this->session->userdata ( 'user_cep' );
        $order->Shipping->Address = new stdClass ();
        $order->Shipping->Address->Street = $this->session->userdata ( 'user_adress' );
        $order->Shipping->Address->Number = $this->session->userdata ( 'user_adress_number' );
        $order->Shipping->Address->Complement = $this->session->userdata ( 'user_adress_comp' );
        $order->Shipping->Address->District = $this->session->userdata ( 'user_adress_district' );
        $order->Shipping->Address->City = $this->session->userdata ( 'user_city' );
        $order->Shipping->Address->State = $this->session->userdata ( 'user_state' );
    } else {
        $order->Shipping->Type = 'WithoutShipping';
    }
}
    
11.11.2015 / 12:06
0

Two problems:

  • You are trying to check a chave and in_array looks for values.
  • You have a% two-dimensional%, and are trying to check the innermost content.

Possible solutions

foreach ($cart_array as $key => $options){
    if(in_array(array('type','Asset'), array_keys($options)){
        # Código aqui
    }
}
    
11.11.2015 / 12:12