0

I am trying to add my data to my array inside my foreach loop.

I have almost done it successfully, except the array is too in-depth.

It's showing array->array->{WHAT I WANT} When I need array->{WHAT I WANT}

Example, I'm needing it to be like:

Array
(
[home] => Array
    (
        [0] => Dashboard\Main@index
        [1] => GET
    )

[home/] => Array
    (
        [0] => Dashboard\Main@index
        [1] => GET
    )
)

When at the moment, it's showing:

Array
(
[0] => Array
    (
        [services/content-writing] => Array
            (
                [0] => Dashboard\Services@contentwriting
                [1] => GET
            )

    )

[1] => Array
    (
        [services/pbn-links] => Array
            (
                [0] => Dashboard\Services@pbnlinks
                [1] => GET
            )

    )
)

The code I'm currently using inside my foreach loop is:

$realArray = array();

// Services exist
if($services)
{
    // Sort them into our array
    foreach ($services as $service) {

        $servicePageName = $service->page_name;
        $serviceName = str_replace(' ', '', strtolower($service->name));

        $realArrayNew = array(
            "services/$servicePageName" => ["Dashboard\Services@$serviceName", 'GET']
        );

        array_push($realArray, $realArrayNew);

        //'home' => ['Dashboard\Main@index', 'GET'],

    }
}

return $realArray;

2 Answers 2

1

The servicePageName variable must be the key field on the realArray to get the results you want.

I'm presuming you input object array looks something like this:

[
    (int) 0 => object(stdClass) {
        name => 'contentwriting'
        page_name => 'content-writing'
    },
    (int) 1 => object(stdClass) {
        name => 'pbnlinks'
        page_name => 'pbn-links'
    }
]

If we do this:

$realArray = [];
if ($services) {
    foreach ($services as $service) {
        $servicePageName = $service->page_name;
        $serviceName = str_replace(' ', '', strtolower($service->name));
        $realArray["services/$servicePageName"] = [
            0 => "Dashboard\Services@$serviceName",
            1 => "GET"
        ];
    }
}

This is what we get on realArray:

[
    'services/content-writing' => [
        (int) 0 => 'Dashboard\Services@contentwriting',
        (int) 1 => 'GET'
    ],
    'services/pbn-links' => [
        (int) 0 => 'Dashboard\Services@pbnlinks',
        (int) 1 => 'GET'
    ]
]
Sign up to request clarification or add additional context in comments.

Comments

0

This portion of the code inserts a new subarray to your main array:

$realArrayNew = array(
    "services/$servicePageName" => ["Dashboard\Services@$serviceName", 'GET']
);

array_push($realArray, $realArrayNew);

Replace it all with:

$realArray["services/$servicePageName"] = ["Dashboard\Services@$serviceName", 'GET'];

That way your top level will have service names as keys.

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.