1

I have multidimensional array which looks like this. This is my snippet down below:

$tyreSpec = array(
    'width_id' => array(
        0 => '8',
        1 => '24'
    ),
    'profile_id' => array(
        0 => '8',
        1 => '13'
    ),
    'diameter_id' => array(
        0 => '4',
        1 => '13'
    ),
    'speed_id' => array(
        0 => '6',
        1 => '13'
    ),
    'load_id' => array(
        0 => '12',
        1 => '31'
    )
);

How can I create an array like this from the above one?

$toDb = array (
    array(
          'width_id' => 8,
          'profile_id' => 8,
          'diameter_id' => 4,           
          'speed_id' => 6,
          'load_id' => 12
    ),
    array(
          'width_id' => 24,
          'profile_id' => 13,
          'diameter_id' => 13,           
          'speed_id' => 13,
          'load_id' => 31
    )
);

This is my code down below I can't get it done:

$ToDb = array();
//$i = 0;
$count = 0;
foreach($tyreSpec as $row ) {
    $count = count($row);
}

for($i = 0; $i < $count; $i++) {
    foreach($tyreSpec as $row) {
        array_push($ToDb, $row[$i]);
    }
}
4
  • 1
    you need a key for each array. What you asking is imposible. How you are making the array? Commented Jul 14, 2016 at 8:41
  • 1
    if you want it dynamically its impossible. Either you have change your array structure or you have to do it statically. Commented Jul 14, 2016 at 8:42
  • the array is coming from a post request Commented Jul 14, 2016 at 8:43
  • change the array structure. so you mean get rid of the string keys and convert the associative array Commented Jul 14, 2016 at 8:44

3 Answers 3

4

You can use two simple loops to achieve your goal:

$toDb = array();
foreach ($tyreSpec as $key=>$val) {
    for ($i=0; $i<count($val); ++$i)
        $toDb[$i][$key] = $val[$i];
}
Sign up to request clarification or add additional context in comments.

4 Comments

i have a question why you are using ++$i not $i++ ?
@Share C memories... in C it saves the space of a temporary variable: for the "math" in a loop ++i or i++ is exactly the same
Thanks for the explanation. :) have a great day
@Share see this for a reference about what I meant in my previous comment
0

I think you are looking for this:

// $source is your input array
$result = [];$result = [];
array_walk($source, function($v,$k) use (&$result){
    $result[0][$k] = $v[0];
    $result[1][$k] = $v[1];
});

print_r($result);

Demo is Here

1 Comment

It works perfectly. i will play with this code thank anyway for the help
0
    $initArray = 
    array (
      'width_id' => 
        array (
          0 => string '8',
          1 => string '24'
        ),
      'profile_id' => 
        array (
          0 => string '8',
          1 => string '13'
        ),
      'diameter_id' => 
        array (
          0 => string '4',
          1 => string '13'
        ), 
      'speed_id' => 
        array (
          0 => string '6',
          1 => string '13' 
       ),
      'load_id' => 
        array (
          0 => string '12', 
          1 => string '31'
        )
    )

$newArray = Array();

foreach ($innitArray as $key => $value){
   $newArray[$key][0] = $innitArray[$key][$vallue][0];
   $newArray[$key][1] = $innitArray[$key][$vallue][1];
}

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.