0

I have to following array:

Array
(
[0] => Array
    (
        [clave_su] => CAD
        [nombre_su] => CENTRO DE APOYO AL DIAGNOSTICO
    )

[1] => Array
    (
        [clave_su] => CADIT
        [nombre_su] => CENTRO DE APOYO AL DIAGNOSTICO E INGENIERIA TISULAR
    )

What i am expecting is this:

Array
(
    [0] => Array
        (
            [0] => CAD
            [1] => CENTRO DE APOYO AL DIAGNOSTICO
        )

    [1] => Array
        (
            [0] => CADIT
            [1] => CENTRO DE APOYO AL DIAGNOSTICO E INGENIERIA TISULAR
        )
)

What i tried is the use of the array_values function of PHP.

But i cant get it work..

Any ideas?

Thanks

5 Answers 5

1

Try this

foreach ($array as $&item) {
    $item = array_values($item);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks!I just added array_push($noAssociativeArray,$item); to create my new array. Good day
1

How about:

foreach ($array as $key => $value) {
    $array[$key] = array_values($value);
}

Comments

0

You can just walk the original array and strip out the keys using array_values(). that might look like this:

// your initial array
$array = array(
    array(
        'clave_su' => 'CAD',
        'nombre_su' => 'CENTRO DE APOYO AL DIAGNOSTICO'
    ),
    array(...),
    ...
);
// transform inner arrays to be numerically indexed
array_walk($array, function(&$sub_array, $key_not_used) {
    $sub_array = array_values($sub_array);
});

2 Comments

Needless step with array_walk. Just array_map the array_values.
@AbraCadaver Yes. That is suitable as well.
0

Unlike the solutions using array_keys, this snippet ensures that the transformation from keys to index is always the same. This means that if a certain key is missing for an entry, or if the definition order isn't the same accross all entries, you always access the same data with a given index.

function array_assoc2index ($input) {
    $output = array();
    $keycount = 0;
    $keys = array();

    foreach ($input as $val) {
        $a = array();
        foreach ($val as $k => $v) {
            if (!isset($keys[$k]))
                $keys[$k] = $keycount++;
            $a[$keys[$k]] = $v;
        }

        $output[] = $a;
    }

    return $output;
}

Comments

0

Easiest one-liner:

$array = array_map('array_values', $array);

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.