0

How can I convert this array The value of the new array index array is the first and the second value is an array as the value for the index. For example, in the new array is indexed hadaf and its value buy And similarly for the others. example hadaf=>buy array first:

array (size=2)
'name' => string 'type' (length=4)
'value' => string 'ویلایی' (length=12)
1 =>
array (size=2)
'name' => string 'hadaf' (length=5)
'value' => string 'buy' (length=8)
2 =>
array (size=2)
'name' => string 'name' (length=4)
'value' => string 'ali' (length=8)
3 =>
array (size=2)
'name' => string 'mobile' (length=6)
'value' => string '444444444' (length=9)
4 =>
array (size=2)
'name' => string 'address' (length=7)
'value' => string 'masshad' (length=8)
3
  • 1
    Show some of your array, and what you expect the new array to look like, don't simply describe it in vague and ambiguous words Commented Nov 28, 2015 at 11:09
  • My best guess, based on your description: $tmpArray = array_chunk($oldArray, 2); $newArray = array_combine($tmpArray[0], $tmpArray[1]); Commented Nov 28, 2015 at 11:10
  • A more detailed explanation of the example? Commented Nov 28, 2015 at 13:06

1 Answer 1

1

Simply use array_column if PHP > 5.5 like as

print_r(array_column($your_array,'value','name'));

Use array_walk

$result = [];
array_walk($your_array,function($v) use (&$result){
    $result[$v['name']] = $v['value'];
});
Sign up to request clarification or add additional context in comments.

2 Comments

undefined function array_column() php is >5.5 . Another method is not available?

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.