Problem with in_array()

Asked

Viewed 1,057 times

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 he only returns Shipping with Withoutshipping.

I forgot some parameter to do in_array correctly?

5 answers

4

I managed to find a way.
You actually use an in_array and an array_column to specify the array.

if(in_array('Asset', array_column($cart_array, 'type'))){}
  • It makes sense, because for the in_array to work a "simple type" is required and the array is complex type, with the array_column, the data type checked by in_array is restricted.

  • 1

    Observing: array_column is only available from PHP 5.5.

1

The problem in my point of view is that Voce is looking for an array inside another one, so I understood that Voce wants to see inside the $cart_arry array if there is an object of type Asset, but the in_array I understand will not help in this, it serves to search for a value inside the array, and not to check the type of the array items, I did not find anything on the internet that does this with 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 what you want you might have to do it another way, run this list and check the guy.

I hope I’ve helped clear your doubt.

  • Hello Cristian, thank you so much for your attention. I understood in parts, the question is how would I run in the array and then check? I thought about conditions and everything. The problem is that by logic, it was much simpler if in_array() returned me already. Because whether or not it is a value that is in the array.

1

There is an easy way to recursively search for a value in a multidimensional array that works in 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';
}

0

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

Before checking, 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';
    }
}

0

Two problems:

  • You’re trying to check a chave whereas the in_array search for values.
  • You have a array two-dimensional, and it’s trying to verify the innermost content.

Possible solutions

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.