Hello i have a problem with convert array from mysql to be identical with this example generate:
'groups' => [
(first column)9480 => [423](next column), //1
42774 => [400], //2
],
I have a two column in mysql First and Next and my question is how i can generate to up example with my two column in mysql
i tried:
$listagildi = $reg->sql->query('select `channelid`,`group` From special_channels');
$testg = $listagildi->fetchAll();
$arr = array('groups' => [$testg], );
Related
I am writing a page that pulls images and image data out of a multidimensional array. I need to be able to click a button that calls a function to sort out the images by tags(IE tag_GlassDoor & tag_GlassWall) - basically to show only images that do or do not have that particular element (in this case im using 0 and 1 for yes and no), such as a glass door. I can currently make that array display the data, but I cant figure out how to sort the data by one of the array keys, or even really the syntax to pull a single value out at will.
$arrImages[] =
[
'img_sm'=>'image1.jpg',
'tag_GlassDoor'=>0,
'tag_GlassWall'=>1,
];
$arrImages[] =
[
'img_sm'=>'image2.jpg',
'tag_GlassDoor'=>1,
'tag_GlassWall'=>1,
];
Filtering is the answer, it can be used to filter one dimensional Arrays and multidimensional arrays.
the general implementation would be something like this:
$arr = array(
array(
'image' => "data",
'hasObject' => 1
),
array(
'image' => "data",
'hasObject' => 0
),
);
$finteredArray = array_filter($arr, function ($r) {
return (bool) $r['hasObject'];
});
print_r($finteredArray);
// it outputs:
// Array ( [0] => Array ( [image] => data [hasObject] => 1 ) )
I'm trying to do something like this:
$variable = Model::where($data)->get();
$data is stored in the database as ['type' => 1, 'status' => 2]
I cannot seem to be able to parse the data from a string to array properly, the best I have gotten close to is
0 => "['type' => 1, 'status' => 2]"
How can I properly parse this array from DB -> variable in php?
I managed to fix it using the following:
$data = "['type' => 1, 'status' => 2]";
$variable = Model::where(eval("return $data;"))->get();
This will work properly.
Cleaned and Updated as #rickdenhaan recommended.
I have an array that has been filled with default data as shown below
$arrivals = array(
"source" => "arrivals",
"data" => array(
0 => array("flight"=>"000","scheduled"=>"0000","city"=>"Geneva","airline"=>"UAL","gate"=>"A00","status"=>"1","remarks"=>"BOARDING"),
1 => array("flight"=>rand(1,2000),"scheduled"=>randomTime(),"city"=>"Baltimore","airline"=>randomAirline(),"gate"=>"A7","status"=>"0","remarks"=>"")
)
);
Now i want to create the same array with data from a table in a loop using the same identifiers such as 'city' but with variable names .
The other part is that the first part of 'data' array is a number which of course in a loop I can use a counter.
The problem is that the Array is created with the static value of ""source" => "arrivals" for which there is only one value for the array and then the arrays of 'data'.
I would like an easy way to set up an array dynamically with a number of records but with the one header of ""source" => "arrivals" and multiple entries for "data' i.e. one element per record I fetch from my table
Thank you
You can do this with a foreach loop in php after you have retrieved your data.
// Get the data from your table source
$data = get_some_data();
$arrivals = [
'source' => 'arrivals',
'data' => []
];
foreach ($data as $city) {
$arrivals['data'][] = [
'flight' => $city['flight'],
'scheduled'=> $city['scheduled'],
'city' => $city['city'],
// etc.
];
}
Alternatively, if you would like to assign the city name as the array key in arrivals, you can replace the first line inside the foreach loop with $arrivals['data'][$city['city']] (or whatever array item holds the city value).
I have 2 arrays. The first array contains the correct word variants and the second array contains incorrect word variants. I want to write them by combining into one as an array, passing in the wrong version of the words to the key and to the value of the correct version of the word. Then write them to a file and use it. I saved the contents of the array in a file, but with a new record to the same file, all the data is cleared and only new entries are re-written and the old entries are deleted automatically. I want that before writing new data to an array, the array is checked for duplication and if such is not written in the file with the array, then let it be written to the file without clearing the previous data of the file. In general, that will select a file or database for storing more than a billion words. Does the database have the best speed or file?
Example first array:
$uncorrect = array
(
0 => "мувосокори",
1 => "мунаггас",
2 => "мангит",
3 => "мангития",
4 => "мунфачир",
5 => "мунфачира",
6 => "манфиатпарасти",
7 => "манфиатчу",
8 => "манфиатчуи",
9 => "манфиатхох",
10 => "манфи",
.....................
);
Example second array:
$correct = array
(
0 => "мувосокорӣ",
1 => "мунағғас",
2 => "манғит",
3 => "манғития",
4 => "мунфаҷир",
5 => "мунфаҷира",
6 => "манфиатпарастӣ",
7 => "манфиатҷӯ",
8 => "манфиатҷӯӣ",
9 => "манфиатхоҳ",
10 => "манфӣ",
.....................
);
I combined two arrays with this code:
$dict = array_combine($uncorrect, $correct);
Example result my array with data here:
$dict = array (
"мувосокори" => "мувосокорӣ",
"мунаггас" => "мунағғас",
"мангит" => "манғит",
"мангития" => "манғития",
"мунфачир" => "мунфаҷир",
"мунфачира" => "мунфаҷира",
"манфиатпарасти" => "манфиатпарастӣ",
"манфиатчу" => "манфиатҷӯ",
"манфиатчуи" => "манфиатҷӯӣ",
"манфиатхох" => "манфиатхоҳ",
"манфи" => "манфӣ",
"минкор" => "минқор",
.....................................
);
I am writing to a file with this code:
file_put_contents("data.json", json_encode($dict));
I will get the array with this code:
$array = json_decode(file_get_contents("data.json"));
You would be better off using a database for this task.
To solve your issue if you decide to keep with the file storage, the reason you are losing old entries is because you forgot to load them before adding new values.
// more or less something like below
$array = json_decode(file_get_contents("data.json"));
$dict = array_combine($incorrect, $correct);
$newArray = array_merge($array, $dict);
file_put_contents("data.json", json_encode($newArray));
This will not be efficient for billions or rows, especially if it's something that gets loaded every time a user loads a page.
Any time you want to add new items just load the file first, then merge the new items in before saving it again. file_put_contents is going to overwrite whatever was there, so you need to get that data before running that function. Something like this:
$array = json_decode(file_get_contents("data.json"));
$newArray = array(
array( "rightWord" => "wrongWord")
);
$finalArray = array_merge($newArray, $array);
file_put_contents("data.json", json_encode($finalArray));
I'm using php with Laravel. I want to update my database with this array, when I excecute this query to update my db, this db updated by 2 times.
It was like array 1, array 1 then array, 2 array 2... What I want just array 1 then array 2. So how do I solve this problem? Thanks.
$chcount = count($kfamily_profile['chname'])-1;
for($x=0;$x<=$chcount;$x++)
{
$childinput[$x] = array(
'child_name' => $kfamily_profile['chname'][$x],
'gender' => $kfamily_profile['chgender'][$x],
'child_pob' => $kfamily_profile['chpob'][$x],
'child_dob' => $kfamily_profile['chdob'][$x]
);
DB::table('kids')
->where('fathers_name','=', $fname)
->where('mothers_name','=', $mname)
->orderBy('child_dob','ASC')
->update($childinput[$x]);
}