简体   繁体   中英

check associative array values with values from another associative array

I'm trying to find the continent code for each phone number. for this, I have an associative array with country phone code and continent code. like this:

Array
(
[0] => Array
    (
        [continent] => EU
        [Phone] => 376
    )

[1] => Array
    (
        [continent] => AS
        [Phone] => 971
    )
   )

I have tried to use this array to check the location of each phone number that I get from this array

 Array
 (
    [0] => Array
    (
   [customer_id] => 1273
   [dialed] => 44414307935, 1768036761
    )

[1] => Array
    (
        [customer_id] => 1536
        [dialed] => 52228169542
    )
   )

I think that I want to get a new associative array because I want to send this data to SQL table later

so I have tried to loop trow the array and check the number but I can't loop trow the phone number array

        foreach ($cells as $row) {
        // Get Every Phone Number Array
            foreach ($row as $key => $val) {
            #List of all phone number arrays:
            // Get cCodes
            foreach ($cCode as $key => $value) {

                if (substr($row['dialed'], 0, strlen($key)) == $key) {
                    $phoneNum[$row['customer_id']] = [
                        $val => $value
                    ];
                }
            }
        }
    }
    print_r($phoneNum);

I'm not able to loop trow the actual phone number arrays.

my dream I getting an array with customer id as the key and the numbers and continent code as values but all I get is

  Array
 (
  [1273] => Array
    (
        [44414307935, 1768036761] => Array
            (
                [continent] => AF
                [Phone] => 225
            )

    )
   )

when I'm adding loop to get all of the phone numbers I'm getting an error

Warning: Invalid argument supplied for foreach()

   foreach ($cells as $row) {
        // Get Every Phone Number Array
        foreach ($row as $key => $val) {
            #List of all phone number arrays:
            // Get cCodes
            foreach ($row['dialed'] as $num) {
                foreach ($cCode as $key => $value) {
                    if (substr($num, 0, strlen($key)) == $key) {
                        $phoneNum[$row['customer_id']] = [
                            $val => $value
                        ];
                    }
                }
            }
        }
    }
    print_r($phoneNum);

Warning: Invalid argument supplied for foreach()

This error means that the variable you're using in the foreach() function isn't an array.

It seems that $row['dialed'] is not an array, rather a string with comma-separated values.

[dialed] => 44414307935, 1768036761

So, before this line:

foreach ($row['dialed'] as $num) {

Add the following:

$row['dialed'] = explode(",", $row['dialed']);

Which convert that string into an array.


Regards your end goal - rethink on WHAT you actually need. As @mickmackusa mentioned, it's not really achievable but there's a better way to accomplish your requirements for sure. Why would you want an array to be as a key to another array in your case? Does it make sense?

Think on a better data structure. Consider the goal of your code and the relations between the data items.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM