Creating two dimentional array in for-loop - php

I want to create an array in a for-loop so i can adjust the size ($i) of the array.
I've tried this:
$array = array();
for($i = 1; $i <= 5; $i++) {
array_push($array,
$i => array(
"id" => "",
"option" => ""
)
);
}
But I get the following error:
Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW) in ...
I'v also tried to make it a string by doing $i."" on line 4 but that doesn't seem to work either. Does anyone know why?

More idiomatic would be:
$array = array();
for($i = 1; $i <= 5; $i++) {
$array[$i] = array(
"id" => "",
"option" => "") ;
}
However note that this will give you array indexes from 1-5. Arrays are usually indexed from 0:
$array = array();
for($i = 0; $i < 5; $i++) {
$array[$i] = array(
"id" => "",
"option" => "") ;
}
But this can be done without specifying the key:
$array = array();
for($i = 1; $i <= 5; $i++) {
$array[] = array(
"id" => "",
"option" => "") ;
}

try this:
$array = array();
for($i = 1; $i <= 5; $i++) {
$array[$i] = array(
"id" => "",
"option" => ""
);
}

Remove the $i =>
$array = array();
for($i = 1; $i <= 5; $i++) {
array_push($array, array(
"id" => "",
"option" => ""
)
);
}

Related

looping array in gridview yii2 to make each column

I have variabel date as array like $date = [1,2,3,....,30]
I want to looping this array like in gridview like
column = [
for($i = 0; $i <= count($date); $i++){
[
label => $date[$i]
value => $date[$i]
]
}
]
Column arrange something like that
$columns =[];
$columns[] = "name";
$columns[] = "email";
for($i = 0; $i <= count($date); $i++){
$columns[] = [
'label' => $date[$i]
'value' => function ($model) use($date,$id){
// Your magic code
}
]
}
And pass it on GridView
columns => $columns

How to loop inside an array inside a loop in PHP

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

php for and foreach loop only storing last value

I am looping though one initial array, and specifying values i want. Then i am storing the values i need in an array. Then i am combining those values into a new array, with keys and values. The new array is only storing the last entry of all the data that has been passed to it.
$exampleArray = array();
for ($i = 0; $i < 100; $i++){
$exampleArray[] = array(
$A1 = $Anotherarray[$i][25],
$A2 = $Anotherarray[$i][26],
$A3 = $Anotherarray[$i][24],
$A4 = $Anotherarray[$i][27],
$A5 = $Anotherarray[$i][28]
);
$secondExample = array();
foreach( $A1 as $i => $val )
{
$secondExample[] = array(
"Field1" => $val,
"Field2" => ucfirst($A2[$i]),
"Field3" => ucfirst($A3[$i]),
"Field4" => ucfirst($A4[$i]),
"Field5" => ucfirst($A5[$i])
);
}
You are declaring $secondExample as a new array on every iteration. Do it like this:
$exampleArray = array();
$secondExample = array();
for ($i = 0; $i < 100; $i++){
$exampleArray[] = array(
$A1 = $Anotherarray[$i][25],
$A2 = $Anotherarray[$i][26],
$A3 = $Anotherarray[$i][24],
$A4 = $Anotherarray[$i][27],
$A5 = $Anotherarray[$i][28]
);
$secondExample[$i] = array();
foreach( $A1 as $j => $val) {
$secondExample[$i][] = array(
"Field1" => $val,
"Field2" => ucfirst($A2[$j]),
"Field3" => ucfirst($A3[$j]),
"Field4" => ucfirst($A4[$j]),
"Field5" => ucfirst($A5[$j])
);
}
As because you are overwriting every time the loop goes.Use array_push
$secondExample = array();
foreach( $A1 as $i => $val )
{
$varArray = array(
"Field1" => $val,
"Field2" => ucfirst($A2[$i]),
"Field3" => ucfirst($A3[$i]),
"Field4" => ucfirst($A4[$i]),
"Field5" => ucfirst($A5[$i])
);
array_push($secondExample, $varArray);
}

JSON encoding from wordpress database values

I need a proper JSON file with "geo_langitude", "geo_latitude", "adress" & "url" values
I have wp database with post_meta "property" - "geo_langitude" "geo_latitude" & "address" in there.
my file is smth like that
<?php
function return_markers($count) {
$query = new WP_Query('post_type=property&posts_per_page=-1&orderby=menu_order&order=DESC&post_status=publish');
$array = array();
$i = 0;
for ($i = 0; $i< $count; $i++) {
$elem = array(
't' => 'string '.$i,
'x' => get_post_meta($post->ID,'geo_latitude',true),
'y' => get_post_meta($post->ID,'geo_longitude',true),
'z' => $i
);
$array[] = $elem;
}
echo json_encode($elem);
};
return_markers($i);
?>
It's my first time using JSON so I have troubles and I don't know where. =(
with random markers work perfectly:
<?php
function return_markers($count) {
$array = array ();
for ($i = 0; $i< $count; $i++) {
$elem = array(
't' => 'string '.$i,
'x' => 23 + (rand ( 0 , 10000 ) / 10000) * 5,
'y' => 56.2 + (rand ( 0 , 10000 ) / 10000) * 0.8,
'url' => '#map_redirect_'.$i,
'z' => $i
);
$array[] = $elem;
}
echo json_encode($array);
};
return_markers(23);
?>

Too long fetch doments from collention in PHP

For example, I have inserted 10K documents in collection:
for ($i = 0; $i < 100; ++$i) {
for ($j = 0; $j < 100; ++$j) {
$collection->insert(array('i' => $i, 'j' => $j));
}
}
I have created Indexes:
pkrs:PRIMARY> db.testTest.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"ns" : "parts.testTest",
"name" : "_id_"
},
{
"v" : 1,
"key" : {
"i" : 1,
"j" : 1
},
"ns" : "parts.testTest",
"name" : "i_1_j_1"
}
]
Then I want to fetch 600 documents with 'OR' condition:
for ($i = 5; $i < 12; ++$i) {
for ($j = 0; $j < 100; ++$j) {
$orArr[] = array('i'=>$i, 'j' => $j);
}
}
$cursor = $collection->find(array('$or' => $orArr));
$cursor->batchSize(1000);
foreach ($cursor as $doc) {
// code
}
And first document fetches about 1.5 second! :(
How can I improve performance?

Categories