0

I have array like below.I want to convert this array but I want to use array function.is there any array function to convert below array

[name] => Array
            (
                [0] => Cash Crops
                [7] => Cereal Crops
                [12] => Vegetables
                [25] => Leafy Vegetables
                [28] => Vine Vegetables
            )

into this array

        [0] => Array
            (
                [name] => Cash crops
            )

        [1] => Array
            (
                [name] => Pulse crops
            )

        [2] => Array
            (
                [name] => Oil seed crops
            )

        [3] => Array
            (
                [name] => Cereal Crops
            )
8
  • What functions have you tried? Commented Dec 15, 2015 at 10:40
  • No one is there any function? Commented Dec 15, 2015 at 10:41
  • What you have tried so far inorder to achieve or you are unaware of PHP array functions Commented Dec 15, 2015 at 10:42
  • I can write logic to convert it but I want array function Commented Dec 15, 2015 at 10:44
  • Post that logic within your question Commented Dec 15, 2015 at 10:45

4 Answers 4

4

Using array_walk applied to the inner name array

$a = array(
    'name' => array(
        'Cash Crops','Cereal Crops','Vegetables','Leafy Vegetables','Vine Vegetables'
    )
);

$result = array();

array_walk($a['name'],
    function($value,$index) use (&$result){
        $result[] = array(
            'name' => $value
        );
});

print_r($result);

Will output

Array
(
    [0] => Array
        (
            [name] => Cash Crops
        )

    [1] => Array
        (
            [name] => Cereal Crops
        )

    [2] => Array
        (
            [name] => Vegetables
        )

    [3] => Array
        (
            [name] => Leafy Vegetables
        )

    [4] => Array
        (
            [name] => Vine Vegetables
        )

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

Comments

2

The best way you can do this using a native php function is by using array_map()

<?php
function twod($n)
{
    $res = array('name' => $n);
    return($res);
}

$a['name'] = array(1, 2, 3, 4, 5);
$new_array = array_map("twod", $a['name']);
print_r($new_array);
?>

Please be sure to let the community know why you want something in a particular way instead of just saying "I can write logic to convert it but I want array function"

7 Comments

If you're going to copy something directly from the manual, at least make sure the information you're using is related to the question.
@Jon forgive my ignorance. Could you kindly explain to me, how this is not related to the question?
You changed the function correctly, but you left the second half of the code directly from the manual and didn't link array_map to the function name that you had changed cube to. ^^ Would also be better if in your array creation, you use the data provided by the question, and show the result in print_r format as well. ^^
Yes, I fixed the "cube" error before commenting that facepalm. Will keep in mind the advice about print_r the result. Thanks again. Cheers!
so is searching for array functions in google
|
1
$oldarray = array()//your array with old data;
$newarray = convertArray($oldarray);

function convertArray($old){
    $new = array();
    foreach($old as $index=>$value)
    {
        $new[]['name'] = $value;
    }
    return $new;
}

This would convert it to your array.

2 Comments

I know logic.I want existing array function in php
I want this function due to writing existing function is good practice rather than writing logic
0

This oneliner works with any 'name':

$tal = array_map(function($x) use($array)  {return array( array_keys($array)[0] => $x);}, $array[array_keys($array)[0]]);

1 Comment

what is that $x stands for

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.