1

I'm in a bind..

This is the output I would like;

Array
(
[country] => Array(
                    [0] => England
                    [1] => Channel Islands
                )
[gor] => Array(
                    [0] => North East
                    [1] => North West
                )
[parliamentaryconstituency] => Array(
                    [0] => Aldershot
                    [1] => Aldridge-Brownhills
                )
)

This is what I currently have;

Array
(
[0] => Array
    (
        [country] => England
    )
[1] => Array
    (
        [country] => Channel Islands
    )
[2] => Array
    (
        [gor] => North East
    )
[3] => Array
    (
        [gor] => North West
    )
[4] => Array
    (
        [parliamentaryconstituency] => Aldershot
    )
[5] => Array
    (
        [parliamentaryconstituency] => Aldridge-Brownhills
    )
)

My code is;

foreach ($input as $key => $value) {
    foreach ($value as $subkey => $subvalue) {
        switch ($key)
        {
            case 'country':
                $country = Country::description($subvalue)->get()->first();
                array_push($new_input, array('country' => $country->description));
              break;                    
            case 'gor':
                $gor = Gor::description($subvalue)->get()->first();
                array_push($new_input, array('gor' => $gor->description));
              break;
            case 'parlc':
                $parliamentaryconstituency = ParliamentaryConstituency::description($subvalue)->get()->first();
                array_push($new_input, array('parliamentaryconstituency' => $parliamentaryconstituency->description));
              break;
        }
    }
}

I considered array_push($new_input['country'], $country->description); and having $new_input['country'] above the foreachs but that will output an empty country array if no countries are selected and I'd prefer it not to appear at all if that's the case.

3 Answers 3

2

You could try this one:

$result = array();
foreach ($input as $key => $value) {
    foreach ($value as $subkey => $subvalue) {
        switch ($key)
        {
            case 'country':
                $country = Country::description($subvalue)->get()->first();
                if ($country) {
                    $result['country'][] = $country;
                }
                break;
            case 'gor':
                $gor = Gor::description($subvalue)->get()->first();
                if ($gor) {
                    $result['gor'][] = $gor;
                }
                break;
            case 'parlc':
                $parliamentaryconstituency = ParliamentaryConstituency::description($subvalue)->get()->first();
                if ($parliamentaryconstituency) {
                    $result['parliamentaryconstituency'][] = $parliamentaryconstituency;
                }
                break;
        }
    }
}
print_r($result);
Sign up to request clarification or add additional context in comments.

Comments

0

array_push always add a new element at the end of the array. Instead, initialize the array with all the keys like this

$result = array("country" => array(), "gor" => array(), etc);

And in your loop assuming the value you want to insert is in $value do

$result[$key][] = $value

Or if you prefer using array_push

array_push($result[$key], $value);

Comments

-1
'$list=array();'


/add array value
'array_push($list,array("name"=>"jone","surname"=>"doe"));'

//get array value
'$list[index][columnname]'

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.