0

I'm getting below output as an array.

$array = 
        Array
    (
        [12] => Array
            (
                [1] => Array
                    (
                        [14] => Array
                            (
                                [0] => Array
                                    (
                                        [name] => Avaya Implementation  Services
                                        [service_id] => 14
                                        [ser_type_id] => 1
                                        [service_desc] =>Avaya Implementation Servic
                                    )

                            )

                    )

            )

    );

I want to print only service_desc array value. and I don't want call like $array[12][1][14][0]['service_desc'];

How can I call particular service_desc array value of the array?

7
  • 1
    you will have to call it as $array[12][1][14][0]['service_desc'];, otherwise you will need to re-factor your array, flat it so to speak, or re-factor the way you are generating the array with; Commented Apr 3, 2017 at 10:10
  • 3
    I would take a close look at whatever code is producing such an ODD array first Commented Apr 3, 2017 at 10:10
  • I thought there is any predefined function to catch particular array value. @hassan Commented Apr 3, 2017 at 10:15
  • You say you don't want to use the numeric indexes, which implies that the structure is likely to change in the future. Which bits are going to stay constant? It's impossible to answer this question without knowing that. Commented Apr 3, 2017 at 10:15
  • You can use array_walk_recursive to get value for a key. But still, be clear with your question so that we can help you out Commented Apr 3, 2017 at 10:16

5 Answers 5

1

As you mentioned that you don't want to call it as $array[12][1][14][0]['service_desc'] you can use extract function which will create variables from your array,

extract($array[12][1][14][0]);
echo $service_desc;

And then you can use your particular key such as service_desc as variable.

Sign up to request clarification or add additional context in comments.

Comments

1

You can try this function: (Please optimize as per your requirements)

$arr ="<YOUR ARRAY>";
$val = "service_desc";
echo removekey($arr, $val);

function removekey($arr, $val) {
    $return = array();
    foreach ($arr as $k => $v) {

        if (is_array($v) && $k !== $val) {

            removekey($v, $val);
            continue;
        }
        if ($k == $val) {

            echo ($arr[$k]);               
            die;
         }
        $return[$k] = $v;
    }
    return $return;
}

Comments

1

You can use array_walk_rescursive to frame a single dimensional array of matching keys:

DEMO

<?php
$array[12][1][14][0]['service_desc']  = 'Avaya Implementation Servic';
$array[12][1][14][0]['service']       = 'dsfasf';
$array[12][1][114][0]['service_desc'] = 'Avaya Implementation Servicasdfdsf';

$searchKey = 'service_desc';

$desiredValues = [];
array_walk_recursive($array, function ($v, $k) use ($searchKey, &$desiredValues) {
    if ($k === $searchKey) {
        $desiredValues[] = $v;
    }
});

print_r($desiredValues);

So this will yield:

Array
(
    [0] => Avaya Implementation Servic
    [1] => Avaya Implementation Servicasdfdsf
)

Comments

1

You might use the array_walk_recursive function.

array_walk_recursive($array, function ($val, $key) {
    if ($key == 'service_desc') print_r($val); 
} );

Instead of the print_r statement, you can collect your data into another structure, which you convey using the use statement, or with the $userdata additional parameter (see http://www.php.net/manual/en/function.array-walk-recursive.php ).

$results = array();
array_walk_recursive($array, function ($val, $key) use (&$results) {
    if ($key == 'service_desc') {
        $results []= $val;
    }
} );

Pay extra care to the & in front of the use (&$results) otherwise your array of results will be considered immutable inside the callback (i.e. all changes discarded).

Comments

1

Convert multidimensional array to single array using iterator_to_array

REF: http://php.net/manual/en/function.iterator-to-array.php

$service_desc= iterator_to_array(new RecursiveIteratorIterator(new RecursiveArrayIterator($your_array)), 0);
print_r($service_desc);

Result:

Array
(
    [name] => Avaya Implementation  Services
    [service_id] => 14
    [ser_type_id] => 1
    [service_desc] =>Avaya Implementation Servic
)

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.