I have simple array from Wordpress manage_posts_columns filter, to change the columns in custom post type admin. The array looks like
$columns = array ('lastname' => 'Lastname', 'firstname' => 'Firstname', 'city' => 'City' );
and I'm adding ID column
$columns['id'] = 'ID';
I would need to move the id element to second position in the array. How can this be done?
Instead, you can use array_unshift to prepend elements onto an array, or array_push to add an element at the end of the array.
To reorder the associative array, you can use array_splice. A good example is here: http://uk.php.net/manual/en/function.array-splice.php#92651
Related
I have to pass a query parameters with different values but absolutely equal keys. I found this \GuzzleHttp\Psr7\build_query(); But it returns only last value. For instance:
$array = [
'test' => '1',
'test' => '2'
]
\GuzzleHttp\Psr7\build_query($array);
//OR
http_query_builder($array);
It returns every time string with the last item.
Does it possible to do that with PHP? Because the side which will take these parameters just can not do anything in their side so I have to pass parameters with the equal keys.
The problem was not with the specific method used, but with how you filled your array to begin with:
$array = [
'test' => '1',
'test' => '2'
]
You can not use the same array key twice; your array only contains one element now, because the second one has overwritten the existing first one under that same key.
Make the test element itself an array, that contains your two values:
$array = [
'test' => ['1', '2']
];
Say I have a form containing an input field with
name="my_options[my_elements][1][name]"
and another with
name="my_options[my_elements][1][category]"
When I submit the form all is well and a new array containing name and category keys/values is added to the my_elements array in the my_options array in the DB.
I can also use name="my_options[my_elements][][name]"
to create a new array within my_options[my_elements]
However I obviously can't then use name="my_options[my_elements][][category]"
as this will put the category value into a further new array.
Is there a way of dynamically keeping track of the position of the new array created with the first [] so that values can be added at that particular level of the multidimensional array?
To clarify, I'm looking to dynamically add elements to the following array:
'my_options' =>
array (size ?)
0 =>
'my_elements' =>
array (size=2)
0 =>
array (size=2)
'name' => 'name 1'
'category' => 'cat 1'
1 =>
array (size=2)
'name' => 'name 2'
'category' => 'cat 2'
`
I wouldn't use array definitions in the html part of your application.
Stick with a input name, like name="category" and in your php file fetch the submitted data and organize it in an array the way you want it.
assuming you use POST as form method:
$category = $_POST['category'];
(don't forget to validate the input)
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 an array that has an array containing more data stored into one of the keys in the original array.
For example:
$array2 = array('some_data');
$array1 = array(
'username' => $username,
'password' => $password,
'data' => $array2);
The array1 can also have multiple occurrences, another words store data for more then one user. How can I add more "data" to the data array within $array1?
I tried doing
$array1[0]['data'][] = array('diff_data');
but I keep getting errors. Is there a better way to do this?
Thank you.
You can add new data to
$array1['data'][] = 'new data';
and you can acces this data like this:
echo ($array1['data'][0]);
Where 0 is the index of the array2 element.
$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