2

i have input array format and i convert into the multi dimensional array

Array
(
    [0] => Array
        (
            [third_party_id] => 2
            [third_party_name] => aardvarkg
            [parameter_id] => 221
            [parameter_name] => new2
            [param_value] => 1
            [sub_param_name] => new2_new2
            [sub_param_value] => 1_1
            [sub_bidder_id] => 72
        )
          [1] => Array
        (
            [third_party_id] => 2
            [third_party_name] => aardvarkg
            [parameter_id] => 222
            [parameter_name] => new
            [param_value] => 1
            [sub_param_name] => new
            [sub_param_value] => 1
            [sub_bidder_id] => 74
        )
    [2] => Array
        (
            [third_party_id] => 3
            [third_party_name] => aard
            [parameter_id] => 221
            [parameter_name] => new2
            [param_value] => 3
            [sub_param_name] => new2
            [sub_param_value] => th
            [sub_bidder_id] => 78
        )
)

i need output is multidimensional array format.parent and child array.

 [0] => Array
        (
            [third_party_id] => 2
            [third_party_name] => aardvarkg
            [param]                =>[parameter_id] => 221
                                  [parameter_name] => new2
                                  [param_value] => 1
                                  [subparam]           => [sub_param_name] => new2_new2
                                                   [sub_param_value] => 1_1
                                                    [sub_bidder_id] => 72
        )

i need the output answer above array format..any helping hands will be appericiated

3 Answers 3

2

Pass your Two Dimensional array as below

foreach($yourarray as $key=>$array)
{ 
$i = 1;
    foreach($array as $names=>$values)
    {
    if($i<=2)
    {
    $multidimension[$key][$names] = $values; 
    }
    if($i>=3 && $i<=5)
    {
        $multidimension[$key]['param'][$names] = $values; 
    }
    if($i>5)
    {
        $multidimension[$key]['param']['subparam'][$names] = $values; 
    }
    $i++;
    }
}

print_r($multidimension); //expected array output
Sign up to request clarification or add additional context in comments.

9 Comments

[0] => Array ( [third_party_id] => 2 [third_party_name] => aardvarkg [param] => Array([parameter_id] => 221 [parameter_name] => new2 [param_value] => 1 [subparam] => [sub_param_name] => new2_new2 [sub_param_value] => 1_1 [sub_bidder_id] => 72 )
is this what you got?
i got multiple third party again and again.i need only one third party id then all values under the inner array
in this input array i have two third party id but it loops five times.i need only two array..
[third_party_id] => 3 dont you need this?
|
0

You can use array_map and array_slice, check the live demo.

$result = array_map(function($v){
  $temp = array_slice($v, 0, 2);
  $temp['param'] = array_slice($v, 2, 3);
  $temp['param']['subparam'] = array_slice($v, 5);
  return $temp;
}, $arr);

Comments

0

I think it should be like this

$a = array
(
    array
        (
           "third_party_id" => 2,
            "third_party_name" => 'aardvarkg',
            "parameter_id" => 221,
            "parameter_name" => 'new2',

              "param" =>  array(
        "param_value" => 1),
              "sub_param" => array(
        "sub_param_name" => 'new2_new2',
        "sub_param_value" => '1_1',
        "sub_bidder_id" => 72)
    )

);
  print_r($a);

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.