I am setting this array manually:
$schools = array(
'Indiana University'=>array(
'initials'=>'IU',
'color'=>'red',
'directory'=>'indiana'
)
);
But it won't echo "IU" when I use:
echo $schools[0][0];
It does show correctly when I do:
print_r($schools);
I'm sure I'm messing up something dumb, but I have no idea what and I've been staring at it for hours. This array is actually part of a larger array with multiple universities, but when I trim it down to just this, it doesn't work.
PHP arrays support two types of keys - numerical and strings.
If you just push a value onto an array, it will use numerical keys by default. E.g.
$schools[] = 'Indiana University';
echo $schools[0]; // Indiana University
However, when you use string keys, you access the array values using the string key. E.g.
$schools = array(
'Indiana University' => array(
'initials' => 'IU',
'color' => 'red',
'directory' => 'indiana'
)
);
echo $schools['Indiana University']['initials']; // UI
Related
I stuck with a problem: I have an array with IDs and want to assign theses IDs to a key of a associative array:
$newlinkcats = array( 'link_id' => $linkcatarray[0], $linkcatarray[1], $linkcatarray[2]);
this works fine, but I don't know how many entries in $linkcatarray. So I would like to loop or similar. But I don't know how.
no push, cause it is no array
no implode, cause it is no string
no =, cause it overrides the value before
Could anyone help?
Thanks
Jim
Why not just implode it ?
$newlinkcats = array(
'link_id' => implode(
',',
$linkcatarray
)
);
Or just do this:
// Suggested by Tularis
$newlinkcats = array(
'link_id' => $linkcatarray
);
If your $linkcatarray array is only comprised of the values you wish to assign to the link_id key, then you can simply point the key at that array:
$newlinkcats = array('link_id' => $linkcatarray);
If that array contains more values that you don't want included, then take a look at array_slice() to only grab the indexes you need:
// Grabs the first 3 values from $linkcatarray
$newlinkcats = array('link_id' => array_slice($linkcatarray, 0, 3));
If your desired indexes aren't contiguous, it may be easier to cherry-pick them and use a new array:
$newlinkcats = array('link_id' => array(
$linkcatarray[7],
$linkcatarray[13],
$linkcatarray[22],
// ...
));
I have a simple session array and I'm pushing page titles in it, as strings:
$_SESSION['sesArray'][] = $pageTitle;
and another predefined associative array with page titles and links:
$assoc=array(array('title' => 'page title', 'link' => 'page link'));
The session array gets flooded with titles so I'm taking out duplicates:
$array1 = array_unique($_SESSION['sesArray']);
My question is: how can I compare the $assoc array against $array1 to check for page titles that exist in both and eliminate them, ending up with another array that contains unique titles along with the link?
I have tried using:
$result= array_diff_key($assoc, $array1 );
But some duplicate titles are indeed removed and some are not.
Any ideas?
ETA data:
$array1= array('Museum', 'Club');
$assoc= array(array('title' => 'Museum', 'link' => 'museum.php' ),
array('title' => 'club', 'link' => 'club.php'));
You are not really doing a diff because an array of arrays is by definition going to have nothing in common with an array of scalars. What you need to do is filter $assoc based on the contents of $array1. Try this:
$array1= array('Museum','Club');
$assoc= array(array('title' => 'Museum', 'link' => 'museum.php' ),
array('title' => 'club', 'link' => 'club.php'));
$fn = function($arr) use ($array1) {
return !in_array($arr['title'], $array1);
};
$result =array_filter($assoc, $fn);
Ah, the infamous tech-interview problem ("compare 2 arrays and to find common entries").
try something along the lines of:
$ass = array_keys($assoc);
foreach($ass as $a)
{
while (isset($_SESSION['sesArray'][$a]))
{
unset($_SESSION['sesArray'][$a]);
}
}
The way PHP associates its tuple arrays allows you to avoid the ugly O(n^2) complexity of this issue.
I have an array of values in PHP looking something like this:
$test = array (
array( 'val' => 2, 'color' => 'blue' ),
array( 'val' => 5, 'color' => 'green' ),
);
I want to go through all elements of $test and add 1 to all val indices. Now I realize a foreach loop could work, but I am looking for something a little more efficient. The array will potentially have 10's of thousands of elements and hundreds of sub-elements.
I am wondering if there is some type of way that PHP can go through and modify just that index throughout the entire array, based on the argument that I set.
you can use array_walk and a closure to do it
array_walk($yourArray,function(&$col){$col['val']++});
I have a multidimensional array or arrays which I also use in my configuration file. The file is also manually edited so I want some of the keys to have fixed position. The code reads configuration file with this array, modifies, insert keys etc and then saves it back. On save I sort the keys but now I found that it is not good enough.
Is there any way to have
the key 'full_name' always as the first key
the key 'version' as second one
and the rest of the keys to be sorted alphabetically?
Sample of the array....
array (
'skroob' =>
array (
'ssh' => 'skroob',
'codebase_path' => '/srv/www/htdocs/imacs/radek/4.0.1',
'ssh_status' => 'ok',
'version' => '4.0.1',
'ssh_last_access' => '2012-Feb-17 10:07:26',
'edu_url' => 'https://testing/imacs/radek/4.0.1',
'full_name' => 'skroob 4.0.1',
),
'testing' =>
array (
'full_name' => 'My beautiful testing server (trunk)',
'version' => 'trunk',
'ssh' => 'testing',
'codebase_path' => '/srv/www/htdocs/imacs/radek/trunk',
'ssh_last_access' => '2012-Feb-17 10:07:26',
'ssh_status' => 'ok',
),
)
Here's one way. It sorts the array alphabetically, by key. Then it reverses the array and unsets the full_name and version keys. Then it adds those keys again, so they get placed at the end of the array. Lastly, it reverses the array again. Kinda hacky but I'm not sure there's a much better way to do what you're asking.
ksort($arr['skroob']);
$arr['skroob'] = array_reverse($arr['skroob']);
$version = $arr['skroob']['version'];
$full_name = $arr['skroob']['full_name'];
unset($arr['skroob']['full_name'], $arr['skroob']['version']);
$arr['skroob']['version'] = $version;
$arr['skroob']['full_name'] = $full_name;
$arr['skroob'] = array_reverse($arr['skroob']);
Another solution is to use uksort and write a small callback function. That would probably look a bit more professional.
$string = "php, photoshop, css";
I'm producing an array from the comma separated values above using the str_getcsv() function:
$array = str_getcsv($string);
Result:
Array ( [0] => php [1] => photoshop [2] => css )
How can I replace the key integers with a string tag for all elements like seen below?
Array ( [tag] => php [tag] => photoshop [tag] => css )
Edit: if not possible what alternative can I apply? I need the array keys to be identical for a dynamic query with multiple OR clauses
e.g.
SELECT * FROM ('posts') WHERE 'tag' LIKE '%php% OR 'tag' LIKE '%photoshop% OR 'tag' LIKE '%css%'
I'm producing the query via a function that uses the array key as a column name and value as criteria.
That is not possible. You can have only one item per key. But in your example, the string "tag" would be the key of every item.
The other way arround would work. So having an array like this:
array('php' => 'tag', 'photoshop' => 'tag', 'css' => 'tag');
This might help you, if you want to save the "type" of each entry in an array. But as all the entries of your array seems to be from the same type, just forget about the "tag" and only store the values in a numeric array.
Or you can use a multidimensional array within the numeric array to save the type:
array(
0 => array( 'type' => 'tag', 'value' => 'php' ),
1 => array( 'type' => 'tag', 'value' => 'photoshop' ),
2 => array( 'type' => 'tag', 'value' => 'css' )
);
But still using just an numeric array should be fine if all the entries have the same type. I can even think of a last one:
array(
'tag' => array('php', 'photoshop', 'css')
);
But even if I repeat myself: Just use an ordinary array and name it something like $tag!
BTW: explode(', ', %string) is the more common function to split a string.
To build SQL statement you might do something like this:
// ... inside you build function
if(is_array($value)){
$sql .= "'".$key."' LIKE '%."implode("%' OR '".$key."' LIKE '%", $value)."%'";
} else {
$sql .= "'".$key."' LIKE '%".$value."%'";
}
This might look confusing but it's much cleaner than runnig into two foreach-loops building the query.
That won't work. Your array keys have to be unique, or subsequent additions will simply overwrite the previous key.
As the others said, keys have to be unique. Otherwise, which element should be returned if you access $arr['tag']? If you now say "all of them", then create a nested array:
$array = array();
$array['tag'] = str_getcsv($string);
The value $array['tag'] will be another array (the one you already have) with numerical keys. This makes, because you have a list of tags and lists can be represented as arrays too.
Understanding arrays is very important if you want to work with PHP, so I suggest to read the array manual.
Assuming you know the size of your array beforehand
$tags = array("tag1","tag2","tag3");
$data = array("php","photoshop","css");
$myarray = array();
for ($i=0; $i<count($data); $i++) {
$myarray[$i] = array($data[$i], $tags[$i]);
}
Then
echo $myarray[0][0] . ", " . $myarray[0][1];
Outputs:
php, tag1