I have a string
$tailored_information="3, 5, 10, 13, 7, 6";
Now I have need to make an array like
$input_array = array("Id" => 3, "Id" => 5);
I am using this but not work cause i cant add key ID
explode(",", $tailored_information)
As been told, you can't have array with the same key, because it is a hash table, which will override the "id" every time.
I suggest you to use simply
explode(", ", $id_array);
or
explode(", ", $another_arr['id']);
like this you will group the data by id...
If you wish to go into some more complicated - you could create your own data structure, which will be non-unique array - where you will divide different values by key...
this way the print version will be whatever you want...
An array has to have unique keys. Also, you will now have spaces in your values
What you could do is explode by ", " and then take that array as your array straight away. If the key you want/need is always "Id" then it doesn't matter anyway.
<?php
$abc = "3, 5, 10, 13, 7, 6";
$new_array = explode(',',$abc);
$new_id_array = array();
foreach($new_array as $key=>$val){;
$new_id_array[$key]['id'] = $val;
}
print_r($new_id_array);
?>
you can not put same key in a array key. so for that you have to create a nested array.and that will solve ur problem and now you can have same array key, but in different arrays.OR
$abc = "3, 5, 10, 13, 7, 6";
$new_array = explode(',',$abc);
foreach($new_array as $key=>$val){
$new_id_array['id_'.$key] = $val;
}
Related
I am making question like 3 * 5 = ? and ? * 5 = 15. To make this, I will save an array variable ($arrayTotalQuestions) with the number 1 to 10 and an array variable ($arrayAnswers)
with the number 5,10,15,20,25,30,35,40,45,50 inside $array1. I will loop these 2 rows 20 times in my table like this:
foreach($array1 as $array1) {
echo $array1[array_rand($array1)];
}
The (array)variables inside the array:
$arrayTotalQuestions = range(1, 10);
$number = 5;
$arrayAnswers = (5, 10, 15, 20, 25, 30, 35, 40, 45, 50);
Now I want to store these 3 variables inside another array named $array1:
$array1 = array(
"<tr><td>$this->arrayTotalQuestions</td> <td>x</td> <td>$this->number</td> <td>=</td> <td><input type='text' name='txtAnswer[$this->arrayTotalQuestions]'></td></tr>",
"<tr><td><input type='text' name='txtAnswer[$this->arrayAnswers]' value=''></td> <td>x</td> <td>$this->number</td> <td>=</td> <td>$this->arrayAnswers</td></tr>"
);
But I am getting the error "array to string conversion...", because of the array variables inside $array1.
How can I solve this?
Like the error says you're trying to convert an array to a string, which isn't possible.
Take a look at this example:
<?php
$array = [1,2,3,4,5];
$newVar = "This is my array: $array";
?>
This results in the error E_NOTICE : type 8 -- Array to string conversion -- at line 3.
You're trying to output an array, which has multiple values in it, as part of your string. PHP doesn't know how you want to do this (should it just list all the array vars? Does it put a space between them? A comma? A comma and a space? etc)
So, instead, you need to convert the array into a string yourself. One way you can do this is to use the join() function in PHP.
join() takes 2 parameters: separator and array and outputs a string which is all the array values separated by your separator.
So the above example becomes:
<?php
$array = [1,2,3,4,5];
$newVar = "This is my array: ".join(", ", $array);
?>
Which outputs This is my array: 1, 2, 3, 4, 5
Updated based on comment
To use 2 or more arrays in the same foreach loop you just need to change it into a for loop:
for($i = 0, $i < count($array1); $i++) {
echo $array1[$i].': '.$array2[$i];
}
There is a way to do it with a foreach loop too, but I find that this isn't quite as clean a way to do this for maintenance (just my preference).
foreach($array1 as $index => $value) {
echo $value.': '.$array2[$index];
}
I have the following fairly simple code, where I need to determine if a certain value exists in an array:
$testvalue = $_GET['testvalue']; // 4
$list = '3, 4, 5';
$array = array($list);
if (in_array($testvalue, $array)) { // Code if found } else { // Code if not found }
Even though it is obvious that the number 4 is in the array, the code returns the code inside the else bracets. What have I done wrong?
Change the third line:
$array = array_map('trim', explode(',',$list));
$array here is:
$array = array('3, 4, 5');
which is not the same as:
$array = array(3, 4, 5);
So, fix the way you are creating this array.. don't do it from a string.
Your array contains just one value, the string 3, 4, 5.
See the example on CodePad.
If you want to convert your string in an array, you can use:
$array = explode(', ', $list);
I have added a space behind the comma, but a safer method would be to use just a comma and then trim all values.
$vals = array(51, 23, 77, 3, 8, 31, 17, 102, 87);
arsort($vals);
From here, how can I get the keys of the 3 first values? If I do $vals[0] it won't work because it'll return me the original [0] key before the arsort.
I want to get the original keys of 102, 87 and 77 after arsort.
Depending on what you need it for, one way is
$keys = array_keys($vals);
$keys[0] will contain the first key.
$vals[$keys[0]] will contain the first value.
An alternate way
$part = array_slice($vals, 0, 3, true);
$part will contain three $key => $value pairs for the first three entries.
And for the first three keys, you can mix and match the above, such as:
$firstThree = array_keys(array_slice($vals, 0, 3, true));
$firstThreeKeys = array_slice(array_keys($vals), 0, 3);
echo join(', ', $firstThreeKeys);
I think I have found a method, maybe not the best however:
reset($arr); $key1=key($arr);
next($arr); $key2=key($arr);
next($arr); $key3=key($arr);
You could use array_keys()?
Alternatively, loop through the sorted array with a foreach and you can still get the keys:
$i = 0;
$numKeysToGet = 3;
$keys = array();
foreach ($vals as $k => $v) if ($i < $numKeysToGet) {
$keys[] = $k;
$i++;
} else break;
// $keys now contains the first three array keys
arsort saves key=>value relationship, so it's usualy used for associative arrays (hash). For your needs try to sort value=>key array instead of your key=>value with the standart sotring function. Otherwise you can use foreach loop (limit it with 3 iterations) to get the keys.
I need to test if one element of an array is in another array.
$array_one = array("gogo", "blabla", "toto");
$array_two = array("stackov", "renaul", "toto");
I would like to know if one element of array_one is in array_two ???
How to test that? Am trying in_array but it seems to have problems.
array_intersect()
$array1 = array("gogo", "blabla", "toto");
$array2 = array("stackov","renaul","toto");
$commonElements = array_intersect($array1,$array2);
var_dump($commonElements);
Try this one:
array_intersect($array_one, $array_two);
Mark's answer should be enough for your problem.
If you ever wish to find the intersect of more than 2 arrays, use this:
$arrays = array(
array(1, 2, 3),
array(2, 4, 6),
array(2, 8, 16)
);
$intersection = call_user_func_array('array_intersect', $arrays);
I have an array that has values like 1, 5, 6, 9, 11, 45, 56, etc. What I'm trying to do is to randomly one value, perhaps 6. Then I want to pick a random value excluding 6 (so no doubles). Then a random value excluding the last two, all from inside an array. Any help? I'm trying to do this without while loops but if they are necessary then so be it.
I suggest the following:
# pick a random key in your array
$rand_key = array_rand($your_array);
# extract the corresponding value
$rand_value = $your_array[$rand_key];
# remove the key-value pair from the array
unset($your_array[$rand_key]);
See: array_rand, unset.
Shuffle the array first and then use it as a stack:
$a = array(1, 5, 6, 9, 11, 45, 56);
shuffle($a);
// now you can have your picks:
$pick = array_pop($a);
$pick = array_pop($a);
$pick = array_pop($a);
$pick = array_pop($a);
...
I would probably shuffle the array and get the first/last x value