How to declare/define an associative array properly in PHP? - php

I wrote a program to define an associative array titled $aFilter and tried to print it but I'm not able to. I tried two ways to achieve this but couldn't succeed. Following are the two ways I tried.
Way 1:
<!DOCTYPE html>
<html>
<body>
<?php
$aFilter = Array
(
['pages'] => 1,
['photo'] => 1,
['link'] => 1,
['event'] => 1,
['friend'] => 1,
['user_status'] => 1,
['poll'] => 1,
['quiz'] => 1,
['market'] => 1,
['apps'] => 1
)
print_r($aFilter);
?>
</body>
</html>
Way 2:
<!DOCTYPE html>
<html>
<body>
<?php
$aFilter = Array
(
['pages'] => 1
['photo'] => 1
['link'] => 1
['event'] => 1
['friend'] => 1
['user_status'] => 1
['poll'] => 1
['quiz'] => 1
['market'] => 1
['apps'] => 1
)
print_r($aFilter);
?>
</body>
</html>
After executing both of the above codes I'm getting blank white screen. No any error or warning. Why so happens? How can I get errors and warnings displayed on my webpage without making any change to php.ini file settings?
Can someone please correct the mistake I'm making and help me?

You forgot an ; after defining the array.
And also don't use [] when defining an array. More info on array's.
$aFilter = Array(
'pages' => 1,
'photo' => 1,
'link' => 1,
'event' => 1,
'friend' => 1,
'user_status' => 1,
'poll' => 1,
'quiz' => 1,
'market' => 1,
'apps' => 1
);
print_r($aFilter);
print_r() displays information about a variable in a way that's readable by humans.
It is not the code you need to write.

Both ways are missing the ; after the array definition and Way 2 is missing ,s after each array element. Also, both ways should use 'elName' => 'elValue', instead of ['elName'] => 'elValue',

The problem are:-
Forgot an ; after defining the array.
When you hard-coded value you need to put indexes without brackets.
So write in this way:-
$aFilter = Array(
'pages' => 1,
'photo' => 1,
'link' => 1,
'event' => 1,
'friend' => 1,
'user_status' => 1,
'poll' => 1,
'quiz' => 1,
'market' => 1,
'apps' => 1
);
print_r($aFilter);

Related

How to merge two different array PHP

I would need to combine two different fields.
In the first field I generate days of the month. I want to list all days of the month.
I would like to add a second field to them, where there are items for each day. But, for example, there are no items on weekends or on other days. Ie. that field two will always have fewer items.
The second field is tightened from the DB.
I would need to do a JOIN like in MySQL for the first field.
It occurred to me that in MySQL it would be possible to make a temporary table with a given month and link it here, but I don't think it's right.
$arrayDate = [0 => '20210401',1 => '20210402',2 => '20210403',3 => '20210404',4 => '20210405',5 => '20210406',6 => '20210407',7 => '20210408',8 => '20210409',9 => '20210410',10 => '20210411',11 => '20210412',12 => '20210413',13 => '20210414',14 => '20210415',15 => '20210416',16 => '20210417',17 => '20210418',18 => '20210419',19 => '20210420',20 => '20210421',21 => '20210422',22 => '20210423',23 => '20210424',24 => '20210425',25 => '20210426',26 => '20210427',27 => '20210428',28 => '20210429',29 => '20210430'];
$arrayItem[35] = ['id' => 35, 'date' => '20210401', 'item' => 'aaaa'];
$arrayItem[36] = ['id' => 36, 'date' => '20210402', 'item' => 'bbbb'];
$arrayItem[37] = ['id' => 36, 'date' => '20210430', 'item' => 'cccc'];
// i need output
20210401 - aaaa
20210402 - bbbb
20210403 - empty
20210404 - empty
...
20210430 - cccc
EDIT: I use nested loops, but I still can't get the right output
foreach ($arrayDate as $date) {
foreach ($arrayItem as $item) {
if ($date == $item['date']) {
bdump($item['date']);
} else {
bdump($date);
}
}
}
bdump($item['date']) = '20210401', '20210402', '20210430'
bdump($date) = '20210401', '20210401', '20210402', '20210402', '20210403', '20210403', '20210403', '20210404', '20210404', '20210404', '20210405', '20210405', '20210405' ....
With array_column you create a array from $arrayItem with date as key.
$dateItem is an array like
array (
20210401 => "aaaa",
20210402 => "bbbb",
20210430 => "cccc",
)
The output you can do with a simple foreach.
$dateItem = array_column($arrayItem,'item','date');
foreach($arrayDate as $date){
echo $date.' '.($dateItem[$date] ?? 'empty')."<br>\n";
}
Note:
With
array_column($arrayItem,null,'date')
you get a two-dimensional array with a date as a key that can be used.
array (
20210401 =>
array (
'id' => 35,
'date' => "20210401",
'item' => "aaaa",
),
20210402 =>
array (
'id' => 36,
'date' => "20210402",
'item' => "bbbb",
),
20210430 =>
array (
'id' => 36,
'date' => "20210430",
'item' => "cccc",
),
)

PHP var_export - elgg_get_entities

I am trying to create an API for the social network Elgg. It's working but I am very puzzled by some results.
This is my API:
function my_get_group() {
$group=elgg_get_entities(array(
types => 'group',
guid=>75,
));
return var_export($group[0], true);
}
and this is the reult
{
status: 0
result: "ElggGroup::__set_state(array( 'url_override' => NULL, 'icon_override' => NULL, 'temp_metadata' => array ( ), 'temp_annotations' => array ( ), 'temp_private_settings' => array ( ), 'volatile' => array ( ), 'tables_split' => 2, 'tables_loaded' => 2, 'attributes' => array ( 'guid' => 75, 'type' => 'group', 'subtype' => 0, 'owner_guid' => 54, 'site_guid' => 1, 'container_guid' => 54, 'access_id' => 2, 'time_created' => 1442385556, 'time_updated' => 1442385557, 'last_action' => 1442455132, 'enabled' => 'yes', 'name' => 'goup1', 'description' => '<p>description1</p>', ), 'valid' => false, ))"}
So far so good.
I can now modify my APIto get only the description, by changing
//return var_export($group[0], true);
return var_export($group[0]->description, true);
which gives me
{
status: 0
result: "'<p>description1</p>'"
}
Now the part that I don't understand is the following. if I modify my code to:
//return var_export($group[0], true);
return var_export($group[0]->briefdescription, true);
I get
{
status: 0
result: "'brief description1'"
}
Can someone explain me where this data comes from? I cannot see any mention of briefdescription when I run
return var_export($group[0], true);
Thanks for your help
bonododo
All of those values come through PHP magic methods __get and __set. The reason you see description in var_export result is that it's an attribute, the briefdescription is a metadata.
Have a look at documentation, especially:
Data model overview
Metadata section
ElggGroup-specific attributes

If column have a row=$variable and in another column on the same row=$variable2 return value 1

Sorry for confusing title. But I don't know how to explain this.(I don't speak that good English)
Here is a picture to make it alittle bit more clear:
http://img823.imageshack.us/img823/8812/edxt.png
If pic_name=328.jpg AND user=myhrmans return value of 1
else if you cant find value user=myhrmans return 0 for example. Anyway I can pull this off?
Thank you :)
Not sure to understand precisely what you mean, but I'm going to make an attempt.
Your query should be written somehow like this one:
SELECT user, pic_name,
IF(user = 'myhrmans' AND pic_name = '328.jpg', 1, 0) AS value
FROM yourtable
WHERE user = myhrmans;
When executed from PHP (and after fetching the values), your query will return this array:
$result = array(
0 => array(
'user' => 'myhrmans',
'pic_name' => '326.jpg',
'value' => 0
),
1 => array(
'user' => 'myhrmans',
'pic_name' => '329.jpg',
'value' => 0
),
2 => array(
'user' => 'myhrmans',
'pic_name' => '328.jpg',
'value' => 1
),
3 => array(
'user' => 'myhrmans',
'pic_name' => '319.jpg',
'value' => 0
)
);
Which, if I understood, approaches what you want.

Pull count of records in a table that have the same value with Codeigniter Active Record

I am working on a site where I have a table with "catches" (people have caught fish with a particular lure). The lure_used category is an integer.
I want to find the users "best lure". So i want to loop through all the user_catches rows and return an array that has each lure and how many times it was used.
So for example we might have
catch_1 | lure_used = 5,
catch_2 | lure_used = 3,
catch_3 | lure_used = 6,
catch_4 | lure_used = 3,
so from that i would like to know that 5 and 6 occur 1 time while 3 occurs twice. Therefore 3 is the most successful lure.
I know that I could probably use "MAX" in mysql queries, but i'm not really farmiliar with it and attempts have failed.
I have been able to create an array of lures used like:
$lures = array(5, 3, 6, 3);
So maybe i could just loop through that and output something like...
5 = 1, 3 = 2, 6 = 1.
I guess i'm grasping at straws haha, anyone have a better idea on how to get this done?
Here are all the fields in the "user_catches" table:
'id' => $row->id,
'user_id' => $row->user_id,
'species' => $row->species,
'season' => $row->season,
'lure_used' => $row->lure_used,
'depth' => $row->depth,
'exact_depth' => $row->exact_depth,
'presentation' => $row->presentation,
'catch_weight' => $row->catch_weight,
'length' => $row->length,
'fishing_from' => $row->fishing_from,
'moon_phase' => $row->moon_phase,
'water_temp' => $row->water_temp,
'water_quality' => $row->water_quality,
'notes' => $row->notes,
'rod' => $row->rod,
'reel' => $row->reel,
'line' => $row->line,
'rig' => $row->rig,
'catch_image' => $row->catch_image,
'weather' => $row->weather,
'temp' => $row->temp,
'humidity' => $row->humidity,
'wind' => $row->wind,
'pressure' => $row->pressure,
'visibility' => $row->visibility,
'location' => $row->location,
'weather_icon' => $row->weather_icon,
'catch_date' => $row->catch_date,
SELECT lure, count (*) as count
from user_catches
GROUP BY lures
ORDER BY count;

Array in a Array SOAP V2 (WS-I Compliance Mode)

$result = $proxy->salesOrderInvoiceCreate((object)array('sessionId' => $sessionId->result, 'itemsQty' => array('order_item_id' => 15, 'qty' => '1')));
$mainarray[];
$itemarray[];
I need multiple of this
array('order_item_id' => 15, 'qty' => '1')
Which means i need a array in a array.
foreach(statement){
array_push($itemarray, "order_item_id", echo $item->product_id;);
array_push($itemarray, "qty", echo $item->qty);
array_push($mainarray, $itemarray);
}
enter code here
Request Example SOAP V2 (WS-I Compliance Mode)
http://www.magentocommerce.com/api/soap/sales/salesOrderInvoice/sales_order_invoice.create.html
In fact i'm also not sure what do i replace the current
array('order_item_id' => 15, 'qty' => '1')
with
array($mainarray) ??
That is not the correct way of using array_push your current $itemarray output will look something like
Array
(
[0] => 'order_item_id'
[1] => '200'
[2] => 'qty'
[3] => '2'
)
I would go back to basics and use something like to generate your multi dimensional array:
$itemarray[] = array("order_item_id" => $item->product_id, "qty" => $item->qty);
array_push($mainarray, $itemarray);
Edit:
Ok I reread your questions, ignore $mainArray.
$result = $proxy->salesOrderInvoiceCreate((object)array('sessionId' => $sessionId->result, 'itemsQty' => $itemarray));
That should work as with the other examples qty/itemsQty show it accepting multikey arrays.

Categories