Is it possible to use conditions within an array:
Uses define('COMPANY_ADDRESS_1','something here'); << if empty don't want to be in the array
$invoice->setFrom(array(COMPANY_NAME,COMPANY_ADDRESS_1,COMPANY_ADDRESS_2,COMPANY_TOWN,COMPANY_COUNTY,COMPANY_POSTCODE));
For example COMPANY_ADDRESS_2 is not set and doesn't show in the array so at the moment the output ends up like:
company name
company address 1
<<<<<<<<<< leaves a gap here
company town
company county
company postcode
The output is fine but if nothing is set for example COMPANY_ADDRESS_2 I want to remove it from the array altogether as it's passed to a PDF generator and currently writing as a blank line.
According to your question i think your array look like below:-
Array
(
[0] => company name
[1] => company address 1
[2] =>
[3] => company town
[4] => company county
[5] => company postcode
)
So you need to do in following manner:-
<?php
$result = array('company name','company address 1','','company town','company county','company postcode');//original array
$newArray = array();
foreach($result as $value){
if($value != ''){
$newArray[] = $value;
}
}
echo "<pre/>";print_r($newArray);
echo "<pre/>";print_r(array_filter($result));
?>
Output:-
Array //null value or empty value removed and array is re-indexed
(
[0] => company name
[1] => company address 1
[2] => company town
[3] => company county
[4] => company postcode
)
Array //null value or empty value removed without re-indexed
(
[0] => company name
[1] => company address 1
[3] => company town
[4] => company county
[5] => company postcode
)
Related
This question already has an answer here:
How to echo the key:value pairs of a sub array? PHP
(1 answer)
Closed 2 years ago.
I add array to mysql database for multilingual(2 and 1 is language id in array) data like this:
{
"2":[
{"title":"french title one","address":"french title one"},
{"title":"french title two","address":"french title two"},
{"title":"french title three","address":"french title three"}
],
"1":[
{"title":"english title one","address":"english title one"},
{"title":"english title two","address":"english title two"},
{"title":"english title three","address":"english title three"}
]
}
in print_r i see this:
Array
(
[2] => Array
(
[0] => Array
(
[title] => french title one
[address] => french address one
)
[1] => Array
(
[title] => french title two
[address] => french address two
)
[2] => Array
(
[title] => french title three
[address] => french address three
)
)
[1] => Array
(
[0] => Array
(
[title] => english title one
[address] => english address one
)
[1] => Array
(
[title] => english title two
[address] => english address two
)
[2] => Array
(
[title] => english title three
[address] => english address three
)
)
)
Now I have language id like this:
$language_id = 1;
I need to show loop(foreach) of data array only for language id 1 like this:
english address one
english address two
english address three
how do can i separate multilingual array?!
Very easy - $array it is your php array
foreach($array[1] as $val){ echo"<a href='$val[title]'>$val[address]</a>";}
/* more flexible */
$language_id = 1;
$href = 'title';
$txt = 'address';
foreach($array[$language_id] as $val){ echo"<a href='$val[$href]'>$val[$txt]</a>";}
If you need only a part - loop this part only.
I have a table called product attribute in which I want to store products attribute name and its value one by one. For example of user selected price as attribute name and 100,200,300 as its values then I want to store price with 100, 200 and 300 with product id. I've array like this:-
[attribute_name] => Array
(
[0] => Color
[1] => weight
[2] => Brand
[3] => Price
)
[Attrvalue] => Array
(
[0] => Black
[1] => 2kg
[2] => 3kg
[3] => 4kg
[4] => Sony
[5] => Samsung
[6] => LG
[7] => 500
[8] => 1000
[9] => 2000
[10] => 9000
[11] => 9500
[12] => 10000
)
How to devide this array in key value pair like for color=>Black in php?
You can name your form inputs like this attr[attrname] eg
for single value attr
for mutiple value attr use
<select name="attr['price'][]" multiple>
<option>...</option>
</select>
This will provide you an array like
array('attr'
=>array('color'=>value),
=>array(
'price'=>array(
0=>value1,
1=>value2
)
)
);
foreach($data as $key=>$value)
{
$keys = "'".implode("',' ", $temp_tablev)."'";
$values = "'".implode("',' ", $temp_tablev)."'";
}
$sql = mysqli_query("INSERT INTO ($keys) VLAUES ($values)") or die(mysqli_error());
If you have a data structure that is handy in PHP, but mysql does not need to see the details, use json_encode and json_decode. Then put the result in a TEXT column.
I have an array of Towns that have no sorting whatsoever. I would like to sort by the [category][3] which is the province and then the [category][0] which is the region and display each Grouped Province with its regions and then towns underneath it. So the following array:
Array
(
[0] => Array
(
[name] => Name One
[id] => 1
[link] => http://mylink1.com
[category] => Array
(
[0] => Region 1
[1] => Town 7
[2] => Country
[3] => Province 2
)
)
[1] => Array
(
[name] => Name Two
[id] => 2
[link] => http://mylink2.com
[category] => Array
(
[0] => Region 1
[1] => Town
[2] => Country
[3] => Province 3
)
)
[2] => Array
(
[[name] => Name Three
[id] => 3
[link] => http://mylink3.com
[category] => Array
(
[0] => Region 1
[1] => Town 5
[2] => Country
[3] => Province 2
)
)
[6] => Array
(
[name] => Name Four
[id] => 4
[link] => http://mylink4.com
[category] => Array
(
[0] => Region 1
[1] => Town 1
[2] => Country
[3] => Province 1
)
)
)
... should end up looking like this:
Country (all the same)
Province 1
- Region 1
- - Town 1 name, id, link
Province 2
- Region 1
- - Town 5 name, id, link
- - Town 7 name, id, link
Province 3
- Region 1
- - Town 1 name, id, link
Province is the Primary Grouping factor, then sorted by Region, the Towns in no particular order but I guess Alphabetically would make sense.
I have managed to sort the array by Category using this reference: Sort Multi-dimensional Array by Value but cannot fathom how to sort any further without referencing the Province specifically in a loop by using its name. i.e.
/// shortened version..
foreach($array as $key=>$value)...
if($value == "Province 1") : do something here with these matches
... etc
Any help would be appreciated.
Take a look at the uasort() function:
http://www.php.net/manual/en/function.uasort.php
I don't think that you can do this in one step.
You can group your values like this:
$grouped = array();
foreach ($data as $group)
{
$grouped[ $group['category'][3] ][ $group['category'][0] ][ $group['category'][1] ] = array(/* ... */);
}
But you will still have to sort every array (and it's sub-arrays) using ksort().
You should check, whether you can get the values already presorted. If you, for example, are using a database to retrieve this values, it would be quite trivial to sort them in the database and bring them in the correct (grouped) structure in your PHP code.
looping through the array I've used a switch for the category I'm looking for and then built another array for each group. from there I can sort by region:
foreach($arr as $town){
switch($town['blogcats']){
case "Province 1" : $province1[] = $town;
break;
case...
etc
}
}
Then each new grouped array can be output:
foreach($province1 as $eachtown) {
echo $newtown['name'];
... and so forth.
}
Within the second loop sorting could be done on the Region.
Each Province is manually referenced in a list on my page which also gives me control over placement.
Howver, this answer could also be improved... but the above works without too much effort.
I'm trying to build a multi array with this structure:
Array
(
[2] => Array //this is the user's unique ID
(
[name] => Jack
[location] => Somerville, Massachusetts, United States
[cars] => Array
(
[10] => Toyota //this is the car's unique ID
[11] => Porsche
)
)
[5] => Array
(
[name] => Luke
[location] => Schwelm, North Rhine-Westphalia, Germany
[cars] => Array
(
[1] => Honda
[2] => VW
[5] => Maserati
)
)
[1] => Array
(
[name] => Jabba
[location] => Denver, Colorado, United States
[cars] => Array
(
[3] => Tesla
)
)
)
I am using this foreach loop but am stuck in getting the cars array embedded within the search data array.
Each user may have more than one car so I would need to loop through all cars for each user, generate that array, and put it in the original foreach loop.
$search_data = array();
foreach ($query->result() as $row) {
$search_data[$row->id] = array(
'name' => $row->name,
'location' => $row->location,
'cars' => array($row->car_id), //this is where I need to insert another array
);
}
return $search_data;
Any suggestions how to do this?
Thanks for helping!
SAMPLE TABLE DATA
USER NAME LOCATION CARS
2 JACK A TOYOTA
2 JACK A PORSCHE
5 LUKE B HONDA
5 LUKE B VW
5 LUKE B MASERATI
1 JABBA C TESLA
It seems that you are creating the array through a database table. So can you please give 2 or more sample data. it'll be easier to give an answer if sample data is there.
Edit:
Well this might not be the best code but I think you'll be able to figure out a better way after seeing this
$id = $row['id'];
if (!isset($search_data[$id])){
$search_data[$id] = array();
}
$search_data[$id]['name'] = $row['name'];
$search_data[$id]['location'] = $row['location'];
if (isset($search_data[$id]['cars'])) {
array_push($search_data[$id]['cars'],$row['cars']);
}else{
$search_data[$id]['cars'] = array($row['cars']); //this is where I need to insert another array
}
I've hit a bit of a problem, I'm currently working on a custom CMS that allows the user to change the websites title, description, keywords and footer, as well as various other settings, which are all kept in one mysql table called site_settings which has two columns
setting and content, the setting column holds the settings name, for example title, and the content holds the information such as "welcome to my website", these are loaded into the site by various queries which works great, the trouble im having is I want to load the information into the form fields in the CMS, which I was hoping to do with the following query
$query = mysql_query("SELECT `setting`, `content` FROM `site_settings`");
and then create an array with the information using:
$content = mysql_fetch_array($query);
but the only way to get the information is to make a while statement that cycles through every row until it reaches the end, but what i want to be able to do is use the setting column in my table as the main identifier for retrieving the content
for example at the moment with the while array my arrays look like this:
Array ( [0] => title [1] => title [2] => title [3] => title )
Array ( [0] => keywords [1] => keywords [2] => keywords [3] => keywords )
Array ( [0] => description [1] => description [2] => description [3] => description )
Array ( [0] => footnote [1] => footnote [2] => footnote [3] => footnote )
Array ( [0] => notice_state [1] => notice_state [2] => 1 [3] => 1 )
Array ( [0] => maintenance_state [1] => maintenance_state [2] => 1 [3] => 1 )
Array ( [0] => notice_message [1] => notice_message [2] => notice [3] => notice )
Array ( [0] => maintenance_message [1] => maintenance_message [2] => maintenance [3] => maintenance )
Array ( [0] => about_us [1] => about_us [2] => about us [3] => about us )
Array ( [0] => article_shorten_length [1] => article_shorten_length [2] => 80 [3] => 80 )
so id have to cycle through them all to retrieve all the information, with the following statement:
while($content = mysql_fetch_array($query)) {
echo $content['content'];
}
but what I'm looking to do is put them all into one array with 1 or a few mysql statements like so
Array (
title -> 'welcome to my website',
description -> 'this is my website',
)
etc...
and just echo in the information by calling the setting column from my array without using a while statement and without a separate mysql query for each row like:
$content['title'];
$content['description'];
Is there a way to do this?
Use a loop with mysql_fetch_row:
while ($row = mysql_fetch_row($query))
{
$content[$row[0]] = $row[1];
}
This avoids reading the whole result set into an array that you then discard. It might be more efficient, therefore.
I am really unsure as to what exactly you want. But if I get your question properly, are you looking for the below:
while($content=mysql_fetch_array($query))
$finalResultObj[$content['setting'] ] = $content['content'];
After this, you have an associative array, and (if I have understood your question properly) you would be able to use it like $finalResultObj['setting'] to get the relevant content.
Are you sure, this is what you want?