'How to add dynamically multiple objects of key value inside an array using PHP?
I am very confused about array of objects keys add dynamically based on for loop. I have searched and tried a lot but i could not get proper solution as i want.
Firstly I would like to share my code please go though with it...
foreach ($allData as $key => $value)
{ foreach ($value as $key2 => $val2)
{
$data =
array( 'metafield'=> array(
"key" => "$key2",
"value" => "$val2",
"type" => "single_line_text_field",
"namespace"=> "meta" )
);
print_r(json_encode($data)
);
echo "<br>";
}
}
With the Help of this code I get output like below
{"metafield":
{"key":"length","value":"12","type":"single_line_text_field","namespace":"meta"}
}
{"metafield":
{"key":"height","value":"6.5","type":"single_line_text_field","namespace":"meta"}
}
{"metafield":
{"key":"waist","value":"33","type":"single_line_text_field","namespace":"meta"}
}
{"metafield":
{"key":"leg","value":"54","type":"single_line_text_field","namespace":"meta"}
}
And I want output like this..
{"metafield":
{"key":"length","value":"12","type":"single_line_text_field","namespace":"meta"}
{"key":"height","value":"6.5","type":"single_line_text_field","namespace":"meta"}
{"key":"waist","value":"33","type":"single_line_text_field","namespace":"meta"}
{"key":"leg","value":"54","type":"single_line_text_field","namespace":"meta"}
}
It would be appreciated for answers and suggestions. THANK YOU
Solution 1:[1]
Well, first, as Simone Rossaini commented, for you to get only one "metafield" you need to put it of the loop.
Second, the way you expect the output means its only one key with an array as value, like:
{"metafield": [
{"key":"length","value":"12","type":"single_line_text_field","namespace":"meta"}
{"key":"height","value":"6.5","type":"single_line_text_field","namespace":"meta"}
{"key":"waist","value":"33","type":"single_line_text_field","namespace":"meta"}
{"key":"leg","value":"54","type":"single_line_text_field","namespace":"meta"}
]}
It's hard to provide a sample code without knowing how your data is structured, but you could put the data into an array and then set is as value to the "metafield" key on your original array, something like:
//How I *imagine* your "$allData" is structured
$allData = array(
["key" => "length", "value" => "12", "type" => "single_line_text_field", "namespace" => "meta"],
["key" => "length", "value" => "6.5", "type" => "single_line_text_field", "namespace" => "meta"],
["key" => "length", "value" => "33", "type" => "single_line_text_field", "namespace" => "meta"],
["key" => "length", "value" => "54", "type" => "single_line_text_field", "namespace" => "meta"]
);
//Defining a array to be the value of the key 'metafield' in your "$data"
$metafield = array();
//Pushing the data from the "$allData" into the "$metafield" array
foreach ($allData as $input) {
// The code below depends on how your data is structured, but it gives the idea
array_push($metafield, $input);
}
//attributing the $metafield array as value to the 'metafield' key in your data
$data = array(
'metafield' => $metafield
);
print(json_encode($data));
Feel free to ask anything :)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
