I have an array and want to create a new numeric array. This looks like this:
$created_old = explode("_", $result[$i]["created"]);
$created_new = array();
$created_new[0] = $created_old[2];
$created_new[1] = $created_old[0];
$created_new[2] = $created_old[1];
$created_new[3] = "";
$created_new[4] = rtrim(explode(":", $created_old[3])[2], ")");
//Get name from the database
$created_new[3] = $name;
$created = implode("_", $created_new);
This version works just fine, but the previous was missing one line, so the code would be this:
$created_old = explode("_", $result[$i]["created"]);
$created_new = array();
$created_new[0] = $created_old[2];
$created_new[1] = $created_old[0];
$created_new[2] = $created_old[1];
//$created_new[3] = ""; - I am missing
$created_new[4] = rtrim(explode(":", $created_old[3])[2], ")");
//Get name from the database
$created_new[3] = $name;
$created = implode("_", $created_new);
In the second code the string $created is in the wrong order. The index 4 and 3 are switched. If it would be an associative array I would understand this but as it is an numeric array I assume the indices to increase numerically and beeing ordered like this. As I have a working version I do not need help to fix this code but rather understand why the code behaves as it does...
Best regards
JRsz
All PHP arrays are associative. There's no such thing as a "numeric array" expect in colloquial speech. A key can be either a string or a number, it doesn't matter. Keys are still ordered by their order of insertion and never implicitly ordered by their value. You would not be surprised by this behaviour I assume:
$arr['a'] = 1;
$arr['c'] = 3;
$arr['b'] = 2;
// ['a' => 1, 'c' => 3, 'b' => 2]
The exact same mechanics are at work in your "numeric array".
If you want to sort your keys, you need to do so explicitly using ksort.
Related
First post here and only just learning PHP, so sorry for what I'm about to ask.
I have a series of arrays with carefully structured names. Within the preceding PHP I Identify the various sections of the name to and use that to create a string which matches the name of the array I need to reference, however I keep getting an array to string error when I try to indirectly address the array.
Is there a way to pass it across as an Array?
i.e.
$Array1A = Array(1,2,3,4);
$Array1B = Array(2,3,4,1);
$Array2A = Array(3,4,1,2);
$Array2B = Array(4,1,2,3);
$Var1 = '2';
$Var2 = 'A';
$ArrayToLookup = 'Array'.$Var1.$Var2; // Returns the String 'Array2A'
$ArrayToUse = ${$ArrayToLookup}; // I get an Array to String error here
$Answer = $ArrayToUse[2]; // I would want this to read off $Array2A[2]
Your code works in PHP 7.4, one thing I would suggest trying is double "$":
$Array1A = Array(1,2,3,4);
$Array1B = Array(2,3,4,1);
$Array2A = Array(3,4,1,2);
$Array2B = Array(4,1,2,3);
$Var1 = '2';
$Var2 = 'A';
$ArrayToLookup = 'Array'.$Var1.$Var2; // Returns the String 'Array2A'
$ArrayToUse = ${$ArrayToLookup}; // <- this works when tested on php 7.4
$ArrayToUse = $$ArrayToLookup; // <- this should work since its 10 years old
$Answer = $ArrayToUse[2]; // I would want this to read off $Array2A[2]
echo $Answer; // returns 1
I have two arrays:
$info = array();
$submitted = array();
I declared an assignment below:
$info['idnumber'] = 10066;
$submitted[$info['idnumber']] = 'Wow';
array_multisort($submitted);
After doing so, displayed $submitted array.
foreach($submitted as $key => $row){
echo $key;
}
Why does it display 0 instead of 10066? I tried tweaking my code to:
$info['idnumber'] = 10066;
$submitted[(string)$info['idnumber']] = 'Wow';
or
$info['idnumber'] = 10066;
$submitted[strval($info['idnumber'])] = 'Wow';
Still it displays 0. What shall I do to display 10066 as the index of the $submitted array?
Update:
I found out it's a known bug of array_multisort, but still it has no solutions. Any idea how to fix ]it?
As you pointed out it is a known behaviour.
The solution was proposed in the discussion
For the moment I'm going to say prefix all your array keys with an extra 0 (or any non-numeric) to force their casting as strings.
When you try to cast integer into string like this:
(string)$info['idnumber']
you still get the integer, because you have a valid number as a string.
So you need to have a string as with some prefix. Prefix can be a 0 or any other non-numeric character. like an i
$info['idnumber'] = '010066';
Or
$info['idnumber'] = 'i00066';
This will return the exact index.
I've splitted a string into array, giving a delimitator. So, this new array created, will contain values that I would want to use as indexes for another given array.
Having a situation like this:
// my given array
$array['key1']['key2']['a_given_key']['some_other_given_key'] = 'blablabl';
// the value of my given array
$value = $array['key1']['key2']['a_given_key']['some_other_given_key'];
$string = "key1;key2";
$keys = explode(";", $string);
I want to call dinamically (during the execution of my PHP script) the value of the given array, but, using as indexes all the values of the array $keys, and in addition appending the indexes ['a_given_key']['some_other_given_key'] of my given array.
I hope I have been clear.
Many thanks.
To make it work you have to use references. Below code should work as you expect:
<?php
$string = "key1;key2;key3;key4";
$keys = explode(";", $string);
$array['key1']['key2']['key3']['key4']['a_given_key']['some_other_given_key'] = 'blablabl';
$ref = & $array;
for ($i=0, $c = count($keys); $i<$c ; ++$i) {
$ref = &$ref[$keys[$i]];
}
echo $ref['a_given_key']['some_other_given_key'];
$value = $ref['a_given_key']['some_other_given_key'];
echo $value;
?>
I would like to add that just after using reference you should unset it using:
unset($ref);
If you don't do this and many lines later you run for example $ref = 2; it will modify your source array so you have to remember about unsetting references just after it's no longer in use.
I have an array that I want to rename so that the values are stored depending on what number the for loop is on. I tried something like this but its giving me an error.
for ($i =0;$i<4;$i++){
$array.$i = array();
push stuff into array;
}
So at the next iteration the array is called array1, then array2 and so forth. What is the best way to do this.
To literally answer your question:
$arrayName = 'array' . $i;
$$arrayName = array();
$$arrayName[] = ...
What you really want is a multidimensional array though:
$array[$i] = array();
$array[$i][] = ...;
You want to use variable variables, in which the double dollar sign indicates that the name of the variable is taken from a variable.
$varname = "array";
for ($i =0;$i<4;$i++){
$newvarname = $varname . $i
$$newvarname = new array()
push stuff into array;
}
I would add that in these cases, a simpler solution is often to use an array in which the desired variable names are indices. So instead of creating $array1, $array2, and so forth, you'd have:
$arrays = array (
'array1' => array(stuff),
'array2' => array(stuff),
'array3' => array(stuff),
'array4' => array(stuff)
}
At least, I find it easier to keep track of.
You should be able to reference the array using the $$ notation for variable variables (see: http://www.php.net/manual/en/language.variables.variable.php).
So, something like this should work (untested):
for ($i =0;$i<4;$i++){
$thisArrayName = 'array'.$i;
$$thisArrayName = array();
push stuff into array;
}
You need array of array
for ($i =0;$i<4;$i++){
$array[$i] = array();
push stuff into array;
}
So, I'm starting a new project and working with php for the first time.
I get that the average definition and functioning of arrays in php is actually pretty much a namevalue combo.
Is there some syntax, API, or other terminology for just a simple list of items?
I.e. inserting something like ['example','example2','example3','example4'] that I can just call based off their index position of the array, without having to go in and modify the syntax to include 0 => 'example', etc...
This is a very shortlived array so im not worried about long term accessibility
php arrays are simple to use. You can insert into an array like:
$array=array('a','b','c'.....);
Or
$array[]="a";
$array[]="b";
$array[]="c";
or
array_push($array, "a");
array_push($array, "b");
array_push($array, "c");
array_push($array, "d");
and call them by their index values:
$array[0];
this will give you a
$yourArray = array('a','b','c');
or
$yourArray[] = 'a';
$yourArray[] = 'b';
$yourArray[] = 'c';
will get you an array with integer index values instead of an associative one..
You still can use array as "classic" arrays in php, just the way you think.
For example :
<?php
$array = array("First", "Second", "Third");
echo $array[1];
?>
You can then add different values <?php $array[] = "Forth"; ?> and it will be indexed in the order you specified it.
Notice that you can still use it as an associative array :
<?php
$array["newValue"] = "Fifth";
$array[1] = "ReplaceTheSecond";
$array[10] = "";
?>
Arrays in PHP can either be based on a key, like 0 or "key" => "value", or values can just be "appended" to the array by using $array[] = 'value'; .
So:
$mine = array();
$mine[] = 'test';
$mine[] = 'test2';
echo $mine[0];
Would produce 'test';
Haven't tested the code.