My config.php contains:
$a1="title";
$b1="text";
$a2="title2";
$b2="text2";
and I have 1.php which includes config.php.
I need to include ($a1 and $b1) or ($a and $b) randomly.
How can I do this? :)
Thank you.
If this data is related, and you need a random set, store it in an array:
$sets = array(
array(
'title' => 'Some title',
'text' => 'Some text here about the title'
),
array(
'title' => 'Some other title',
'text' => 'Some other text here about the title'
)
);
With this array, we have two indexes that we can choose from:
$sets[0]; // Title: Some title, Text: Some text here about the title
$sets[1]; // Title: Some other title, Text: Some other text here about the title
If we want one of those, randomly, we could do the following:
$index = array_rand( $sets, 1 );
This will select either 0, or 1. With more entries in our array, the potential for this number to be larger also increases. We can then use it to grab one of our sets of data:
$dataSet = $sets[ $index ];
And then display the output:
echo $dataSet['title'];
echo $dataSet['text'];
Related
<?
$categoriesID = array("popular","old");
$product => array (
Product 1
'categoryID' => $categoriesID[1],
'Name' => 'Product One',
Product 2
'categoryID' => $categoriesID[2],
'Name' => 'Product Two',
Product 3
'categoryID' => $categoriesID[2],
'Name' => 'Product Two',
Product 4
'categoryID' => $categoriesID[2],
'Name' => 'Product Two',
);
How can I loop through this to reflect that product 1 belongs to category 1, product 2 belongs to category 2, product 3 belongs to category 2 and so on?
I tried the following but no luck..
foreach($product as $key => $pro){
var_dump($categoriesID[$key]);
}
I would really appreciated any suggestions or how what i'm doing wrong.The goal is to insert the relationship into a database table where in order to insert a product a category_id is required.
Your arrays are not written correctly. You got a multi dimensional array here (arrays inside of an array). Read this to understand how they are written and how you can work with them: http://php.net/manual/en/language.types.array.php
If your categories are numeric you should also consider to use numeric values: 1 instead of '1' inside of the $categoriesID array or depending on the database auto casting capability you will get issues inserting strings as decimals.
Here is your given code modified as working example. Ive changed the var_dump output for better readability of the result.
Ive also changed the array indexes you have used since arrays start at 0. If you need the numbers still to start at 1 you could add some nonsense value at the beginning of the array or subtract 1 when accessing the array. Keep in mind that this is an quick & dirty solution to the given problem.
Nevertheless as Patrick Q said you should consider some introduction to PHP.
<?php
$categoriesID = array('1','2');
$product = array (
array(
'categoryID' => $categoriesID[0],
'Name' => 'Product One',
),
array(
'categoryID' => $categoriesID[1],
'Name' => 'Product Two',
),
array(
'categoryID' => $categoriesID[1],
'Name' => 'Product Two',
),
array(
'categoryID' => $categoriesID[1],
'Name' => 'Product Two',
)
);
foreach($product as $key => $value){
echo var_export($value, true) . '<br>';
}
You could further edit Mariusz's answer to do something like this:
foreach($product as $item){
echo $item['Name'].' - '.$item['categoryID'].'<br>';
}
This would give you easy access to both product name and category ID.
I have an array with key and value pair. I'm building this array dynamically and below is the code.
$x[] = array('type_name' => $value->name,
'percentage'=> intval($percentage));
My intention is to get the maximum value and for that I do
max($x);
However it is returning the wrong value actually the lowest value. Following is my array. Any help would be awesome.
$x = array(
array(
'type_name' => 'type 1'
'percentage' => 10,
),
array(
'type_name' => 'type 2'
'percentage' => 15,
),
array(
'type_name' => 'type 3'
'percentage' => 45,
),
);
Thanks is advance.
From php max() documentation :
// Multiple arrays of the same length are compared from left to right
It means that if you want to compare "percentage" values first instead of "type_name" values, you'll have to change their order in the array.
So, you could build your array like this ("percentage" comes first) and it should work :
$x[] = array(
'percentage'=> intval($percentage),
'type_name' => $value->name
);
For example :
$x = array(
array(
'percentage' => 10,
'type_name' => 'type 1'
),
array(
'percentage' => 15,
'type_name' => 'type 2'
),
array(
'percentage' => 45,
'type_name' => 'type 3'
),
array(
'percentage' => 25,
'type_name' => 'type 4'
)
);
print_r(max($x));
Output :
Array
(
[percentage] => 45
[type_name] => type 3
)
Hope it helps.
You need to read how the max compares against different types of data. In your case, you are trying to compare against one of the array item i.e. percentage inside one of the item so the function max does not know to do this.
There is an example by Revo in the manual which shows you how to do this.
You are creating an array of arrays. max doesn’t know that your arrays should be compared by the 'percentage' key, so it can’t be used here.
Instead, find the maximum value yourself. For example, like this:
$maxPercentage = false;
foreach ($x as $item) {
if ($maxPercentage === false || $item['percentage'] > $maxPercentage) {
$maxPercentage = $item['percentage'];
}
}
Now, $maxPercentage will store maximum percentage. Of, if you want an item with maximum percentage, get it like this:
$maxPercentage = false;
$maxItem = false;
foreach ($x as $item) {
if ($maxPercentage === false || $item['percentage'] > $maxPercentage) {
$maxPercentage = $item['percentage'];
$maxItem = $item;
}
}
I'm working on trying to figure out how to show a search result from closest match to least closest.
Let's assume this is the multidimensional array of results. You will notice that there are arrays with the same "id", but have different "categories". I'm pretending this is a one-to-many relationship. So I'm assuming, for 1 "id", a user might have tagged it to 3 different relevant categories.
$results[] = array(
'id' => 1 ,
'text' => 'this is my first post',
'category' => 'blue'
);
$results[] = array(
'id' => 1 ,
'text' => 'this is my first post',
'category' => 'green'
);
$results[] = array(
'id' => 1 ,
'text' => 'this is my first post',
'category' => 'purple'
);
$results[] = array(
'id' => 2 ,
'text' => 'this is my second post',
'category' => 'blue'
);
$results[] = array(
'id' => 2 ,
'text' => 'this is my second post',
'category' => 'green'
);
Now, let's assume there are criteria that the user selected. I'll show it in array form:
$criterias = array('blue', 'green', 'purple');
Using this example, that means the $results "id" of 1 should show up first, and I want to show it's "text". This is because it scored 3 out of 3 (based on matching the criteria that was set in $criterias). Then following this logic the $results "id" of 2 should show up second because it only scored a 2 out of 3.
The final form what what I'm looking to do is be able to echo out the "text" value from highest score to lowest.
My level of programming in PHP is intermediate, so if you could please demonstrate a less complex solution that an intermediate could understand that would be great.
What I tried and didn't get to work was trying to first try to score it and put it into another multidimensional array and sort it, then echo it, but I couldn't get it to work.
I have an array $options:
$options = ('value' => '87', 'text' => 'Accessorize', 'image' =>'accessorize.ico'),('value' => '35', 'text' => 'Adams Kids', 'image' =>'AdamsKids.ico');
After using json_encode produce an output string like this:
[{"value":"87","text":"Accessorize","image":"accessorize.ico"},{"value":"35","text":"Adams Kids","image":"AdamsKids.ico"}]
but what I want is to add an entry at the beginning to have:
[{"value":"0","text":"- Select Shop -","image":""},{"value":"87","text":"Accessorize","image":"accessorize.ico"},{"value":"35","text":"Adams Kids","image":"AdamsKids.ico"}]
I created the following array:
$first = array('value' => '0', 'text' => '- Select Shop -', 'image' =>'');
and I used the following cancatenation methods:
$options2 = array_merge($first, $options);
$options2 = $first + $options;
but both produce the following:
{"value":"0","text":"- Select Shop -","image":"","0":{"value":"87","text":"Accessorize","image":"accessorize.ico"},"1":{"value":"35","text":"Adams Kids","image":"AdamsKids.ico"},"2":{"value":"92","text":"Alex and Alexa","image":"alexandalexa.ico"}}
which contains these incremental numerical values (the actual array contains about 200 items).
How can I add the first line to get the desired uotput, i.e.:
[{"value":"0","text":"- Select Shop -","image":""},{"value":"87","text":"Accessorize","image":"accessorize.ico"},{"value":"35","text":"Adams Kids","image":"AdamsKids.ico"}]
$options2 = array_unshift($options,$first);
array_unshift()
I currently have an array set up like this:
$u_id= array(
array(
NUM=>'2770', DESC=>'description one'
),
array(
NUM=>'33356', DESC=>'description two'
),
array(
NUM=>'13576', DESC=>'description three'
),
array(
NUM=>'14141', DESC=>'description four'
)
);
I need to be able to pass a number through this array as $num (corresponding to a NUM=>'' in the array), and store the corresponding DESC=>'' as a string. For example, searching for "2770" would return "description one".
What would be the best way to go about doing this?
Are you constrained to this array structure? Because a more efficient structure would be to just do
$u_id= array(
'2770' => 'description one',
'33356' => 'description two',
'13576' => 'description three',
'14141' => 'description four'
);
That is to say, you just assume that the key is the number and the value is the description, rather than naming them explicitly. Then the code to find the correct description is just $u_id[2770] (or whichever).
If that's not acceptable, you could also do
$u_id= array(
'2770' => array(
NUM=>'2770', DESC=>'description one'
),
'33356' => array(
NUM=>'33356', DESC=>'description two'
),
'13576' => array(
NUM=>'13576', DESC=>'description three'
),
'14141' => array(
NUM=>'14141', DESC=>'description four'
)
);
That is, the number is also used as the key to find the correct pair. The code to find the correct description becomes $u_id[2770]["NUM"].
In either of these scenarios, finding a given description from the number is a single step. If you can't change the array structure, though, then you'd have to loop through the array to check (which could take as many steps as there are items in the array).
foreach($arrays as $arr){
if($arr['NUM']==$num){
return $arr['DESC'];
}
}