I'm working in PHP.
I have a dynamic number of arrays called 'placeholder0, placeholder1, placeholder2....' and I want to json_encode them
$encode = json_encode(array('tabs' => $tabs, 'placeholder0' => $placeholder0, 'placeholder1' => $placeholder1, 'placeholder2' => $placeholder2 ..... ));
The number of values inside the 'tabs' array is the same as the number of 'placeholder' arrays.
How can I do this?
$result = array('tabs' => $tabs);
for ($i = 0; $i < count($tabs); $i++) {
$result['placeholder' . $i] = ${'placeholder' . $i};
}
file_put_contents($jsonLocation, json_encode($result));
Related
I'm Getting some arrays from some wordpress custom fields:
$content = array(get_post_meta($postId, 'content'));
$media = array(get_post_meta($postId, 'media'));
$yt = array(get_post_meta($postId, 'youtube'));
I then need to have it printing in sequence, like:
media
content
LInk
Embed
And repeat the sequence for each value
media
content
LInk
Embed
For the sequence I'd use this:
echo '<ul>';
for ($i = 0; $i < count($all_array['media']); $i++) {
for ($j = 0; $j < count($all_array['content']); $j++) {
for ($k = 0; $k < count($all_array['youtube']); $k++) {
echo '<li>media->' . $all_array['media'][$i] . '</li>';
echo '<li>content->' . $all_array['content'][$j] . '</li>';
echo '<li>link->' . $all_array['link'][$k] . '</li>';
}
}
}
echo '</ul>';
But I'm doing something wrong with the merging of the 3 fields as if I do a var_dump before to run the for bit, like
echo '<pre>' . var_export($all_array, true) . '</pre>';
Then this is what I get and I cannot iterate as I wish:
array (
0 =>
array (
0 =>
array (
0 => '
brother
',
1 => '
Lorem
',
2 => '
End it
',
),
1 =>
array (
0 => '337',
1 => '339',
),
2 =>
array (
0 => 'https://www.youtube.com/watch?v=94q6fzbJUfg',
),
),
)
Literally the layout in html that I'm looking for is:
image
content
link
image
content
link
...
UPDATE
This how I am merging the arrays:
foreach ( $content as $idx => $val ) {
$all_array[] = [ $val, $media[$idx], $yt[$idx] ];
}
This is the associative array how it looks like:
Content:
array (
0 =>
array (
0 => '
brother
',
1 => '
Lorem
',
2 => '
End it
',
),
)
Media
array (
0 =>
array (
0 => '337',
1 => '339',
),
)
Youtube
array (
0 =>
array (
0 => 'https://www.youtube.com/watch?v=94q6fzbJUfg',
),
)
The way I've resolved it is first I calculate the tot. items within each array, then I get the array with max items and loop and add the items in sequence:
//GET CUSTOM FIELDS
$content = get_post_meta($post_to_edit->ID, 'content', false);
$media = get_post_meta($post_to_edit->ID, 'media', false);
$yt = get_post_meta($post_to_edit->ID, 'youtube', false);
$max = max(count($content), count($media), count($yt));
$combined = [];
//
// CREATE CUSTOM FIELDS UNIQUE ARRAY
for($i = 0; $i <= $max; $i++) {
if(isset($media[$i])) {
$combined[] = ["type" => "media", "value" => $media[$i]];
}
if(isset($content[$i])) {
$combined[] = ["type" => "content", "value" => $content[$i]];
}
if(isset($yt[$i])) {
$combined[] = ["type" => "youtube", "value" => $yt[$i]];
}
}
Finally I can loop:
foreach ($combined as $key => $val) {
if($val['type'] === "media") {
...
}
if($val['type'] === "content") {
...
You don't need to merge the arrays together. It will work fine in separate arrays. However, your for loops don't have the right logic. Try:
for ($i = 0; $i < count($media); $i++) {
for ($j = 0; $j < count($media[$i]); $j++) {
echo '<li>media->' . $media[$i][$j] . '</li>';
}
for ($j = 0; $j < count($content[$i]); $j++) {
echo '<li>content->' . $content[$i][$j] . '</li>';
}
for ($j = 0; $j < count($youtube[$i]); $j++) {
echo '<li>link->' . $youtube[$i][$j] . '</li>';
}
}
Basically, what I'm trying to do is loop inside a loop and in arrays. I have no idea how to do it (that's why I'm here) and I've been testing many things.
As it sounds very confused I write down the code how it would be, but clearly that's not correct.
This below is what I've tried.
$whatever->insertOne(
['name' => 'whatever',
'data' => array(
for ($i = 0 ; $i < 50 ; $i++) { // <-first loop
'something' => array(
for ($j = 0 ; $j < 50 ; $j++) { // <-second loop
'somevalue' => array(
'date' => $date,
'value' => mt_rand(0,200)
)
}
)
}
)
]);
Try this out:
$data = array();
for ($j = 0 ; $j < 50 ; $j++) {
for ($i = 0 ; $i < 50 ; $i++) {
$data[$j]['something'][$i]['date'] => $date;
$data[$j]['something'][$i]['value'] => mt_rand(0,200);
}
}
$whatever->insertOne(['name' => 'whatever','data' => $data]);
Loop need to be outside to create final array. Don't add loop inside an array.
You probably need something like below:-
$data = ['name' => 'whatever'];
for ($i = 0 ; $i < 50 ; $i++) {
for ($j = 0 ; $j < 50 ; $j++) {
$data[$i]['something'][$j]=['somevalue' => array('date' => $date,'value' => mt_rand(0,200));
}
}
$whatever->insertOne($data);
Note:- you can print your array before insertOne() to check that you are getting array of correct format or some more manipulation needed.Thanks
I am trying to create an associative array using php. My desired output is
Array
(
[key] => fl_0_sq
),
Array
(
[key] => fl_1_sq
)
The code is
$max_val = 2;
for($i=0; $i<$max_val; $i++)
{
$flr_arr .= "array('key' => 'fl_".$i."_sq'),";
}
print_r($flr_arr);
Output is
array('key' => 'fl_0_sq'),array('key' => 'fl_1_sq'),
Now the issue is that it has become a string instead of an array. Is it at all possible to create a array structure like the desired output. Any help is highly appreciated.
You could do this:
<?php
$flr_arr = [];
$max_val = 2;
for ($i = 0; $i < $max_val; $i++) {
$flr_arr[][key] = 'fl_' . $i . '_sq';
}
$output = "<pre>";
foreach ($flr_arr as $i => $flr_arr_item) {
$output .= print_r($flr_arr_item, true);
if($i < count($flr_arr)-1){
$output = substr($output, 0, -1) . ",\n";
}
}
$output .= "</pre>";
echo $output;
The output:
Array
(
[key] => fl_0_sq
),
Array
(
[key] => fl_1_sq
)
I'm not exactly sure what you want to do, but your output could be done by this:
$max_val = 2;
for($i=0; $i<$max_val; $i++)
{
$flr_arr = [];
$flr_arr['key'] = 'fl_".$i."_sq';
print_r($flr_arr);
}
You're declaring a string and concatenating it. You want to add elements to an array. You also can't create multiple arrays with the same name. What you can do, though is a 2D array:
$flr_arr[] = array("key"=>"fl_$i_sq");
Note the lack of quotes around array(). The "[]" syntax adds a new element to the end of the array. The output would be -
array(array('key' => 'fl_0_sq'),array('key' => 'fl_1_sq'))
Please can somebody help me with a solution to make a a string list of array like:
$string=array1, array2, .........., arrayn.
I need to generate this list dynamically.
Now I send it each array in list but I want to increase the numbers and I wonder how to put arrays dynamically inside the list.
In my code I have something like this:
$array[$j]
I need to create a list like this:
$string=$string.','.$array[$j]
...but it doesn't work. I can't send it as a multidimensional array.
Does anyone have any ideas?
if you are talking about a multidimensional array:
$container[0] = $string;
$container[1] = $array;
$container[2] = $my_other_array;
// ...
then for example $container[1] will contain your $array and with $container[1][$j] you can access the key $j in your original array.
My code is
$Copii = [];
$Config_Camere = [];
$nrCamere = $array['Camere'];
for ($i = 1; $i <= $nrCamere; $i++) {
if ($array['Copii_Cam' . $i] >= 1) {
for ($j = 1; $j <= $array['Copii_Cam' . $i]; $j++) {
$Copii[$j] = array_combine(['AgeQualifyingCode', 'Count', 'Age'], ['c', '1', $array['varstaCopil' . $j . '_Cam' . $i]]);
}
if ($array['Copii_Cam' . $i] == 2) {
$Config_Camere['RoomRequest'] = ['IndexNumber' => $i, 'GuestsCount' => ['GuestCount' => [['AgeQualifyingCode' => 'a', 'Count' => $array['Adulti_Cam' . $i]], $Copii[1], $Copii[2]]]];
} else if ($array['Copii_Cam' . $i] == 1) {
$Config_Camere['RoomRequest'] = ['IndexNumber' => $i, 'GuestsCount' => ['GuestCount' => [['AgeQualifyingCode' => 'a', 'Count' => $array['Adulti_Cam' . $i]], $Copii[1]]]];
}
} else {
$Config_Camere['RoomRequest'] = ['IndexNumber' => $i, 'GuestsCount' => ['GuestCount' => ['AgeQualifyingCode' => 'a', 'Count' => $array['Adulti_Cam' . $i]]]];
}
}
And if you see i want to generate dinamicaly the array $Copii
Thanks a lot
I need to overlap data from multiple associative arrays with the following considerations:
If a matching key exists, overwrite it
If a key exists but doesn't match, append new value to that element
If neither of the above, create an element to store the value
Take for example the following structures:
<?php
for ($i = 0; $i < 10; $i++) {
$table["table_$i"] = array(
"cell_0" => array(
'row' => 12,
'column' => 5
)
);
}
for ($i = 4; $i < 12; $i++) {
$table["table_$i"] = array(
"cell_0" => array(
'row' => 9,
'column' => 8
)
);
}
for ($i = 5; $i < 15; $i++) {
$table["table_$i"] = array(
"cell_1" => array(
'row' => 4,
'column' => 1
)
);
}
?>
The desired output would look like this:
{"table_0":{"cell_0":{"row":12,"column":5}},"table_1":{"cell_0":{"row":12,"column":5}},"table_2":{"cell_0":{"row":12,"column":5}},"table_3":{"cell_0":{"row":12,"column":5}},"table_4":{"cell_0":{"row":9,"column":8}},"table_5":{"cell_0":{"row":9,"column":8},"cell_1":{"row":4,"column":1}},"table_6":{"cell_0":{"row":9,"column":8},"cell_1":{"row":4,"column":1}},"table_7":{"cell_1":{"row":4,"column":1}},"table_8":{"cell_0":{"row":9,"column":8},"cell_1":{"row":4,"column":1}},"table_9":{"cell_0":{"row":9,"column":8},"cell_1":{"row":4,"column":1}},"table_10":{"cell_0":{"row":9,"column":8},"cell_1":{"row":4,"column":1}},"table_11":{"cell_0":{"row":9,"column":8},"cell_1":{"row":4,"column":1}},"table_12":{"cell_1":{"row":4,"column":1}},"table_13":{"cell_1":{"row":4,"column":1}},"table_14":{"cell_1":{"row":4,"column":1}}}
Take note from the desired output that the value of cell_0 doesn't replace the value of cell_1: I couldn't get the desired output using array_merge() in this case.
Any help would be appreciated--thanks!
Check array_merge and array_unique php functions.