0

I have this array with city codes:

$aryCityCodes = array ("LND", "NY");

And this other array with user data:

$ary = array (
    array("John", "LND","London"),
    array("Mary", "NY","New York"),
    array("Larry", "AMS","Amsterdam")
); 

I need to end up with an array like $ary but if the city code in $ary is contained in $aryCityCodes the name of the city in $ary has to be followed by "prime city"

So far, this is what I've done:

   if($aryCityCodes)
   {
    if (count($ary) > 0)
    {
        foreach($ary as $item) 
        {

            $bAux= false;
            foreach ($item as $key => $value) 
            { 
                foreach ($aryCityCodes as $aCityCode)
                {

                    if ($aCityCode == $value)
                    {
                        $bAux = true; 
                    }
                    if($bAux)
                    {
                            $value = $value.' prime city'; 

                   }
                }

            }
        }
    }
   }

But this does not work, it does not change the name of the city

What am I doing wrong?

Thanks!

2
  • What is your expected output? I can't understand from your question Commented Jul 2, 2016 at 12:31
  • as &$item and as &$value Commented Jul 2, 2016 at 12:32

3 Answers 3

2

I think I see what you need:

if($aryCityCodes) {
    if (count($ary) > 0) {
        foreach ($ary as $key => $row) {
            if (in_array($row[1], $aryCityCodes)) {
                $ary[$key][2] .= ' prime city';
            }
        }
    }
}

Demo : https://eval.in/599514

Check output :

Array
(
    [0] => Array
        (
            [0] => John
            [1] => LND
            [2] => London prime city
        )

    [1] => Array
        (
            [0] => Mary
            [1] => NY
            [2] => New York prime city
        )

    [2] => Array
        (
            [0] => Larry
            [1] => AMS
            [2] => Amsterdam
        )
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for adding demo, babu
@Miguel Mas, take a closer look at this solition, particulary at the foreach loop. To change value of the source array, he using $ary[$key][2], not the $row as you do in your example.
0

foreach is working on a copy of your data, if you dont explicitly specify it should be "by reference".

Change both foreaches like so:

    foreach($ary as &$item) 

And

        foreach ($item as $key => &$value) 

Comments

0

Create new array, look in the arrCityCodes array for match (where you can also add strict matching) and append the value if a match is found.

$newArray =[];
foreach ($ary as $row) {
    if (in_array($row[1], $arrCityCodes)) {
        $newArray[] = [
            $row[0], $row[1], $row[2] . ' prime city'
        ];
     } else {
        $newArray[] = [
            $row[0], $row[1], $row[2]
        ];
     }
}

The benefit here is that you still have access to the original object which can be handy sometimes.

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.