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.
Related
I have the following array that is supposed to only be keys:
$keys = ['mod_4_key'];
and the bigger array which contains a lot of information:
$big_array = [ 'mod_4_key' => ['old' => '', 'info' => ''], 'mod_5_key' => ..]
I would like to, based on what is inside $keys generate a new array with the information from $big_array, as such, if we are to compute the "non-difference" between the arrays, the output should be:
$final_array = [ 'mod_4_key' => ['old' => '', 'info' => '']]
I achieved this using a classic foreach but I was wondering if there was no in-built way to achieve this.
You may be better off with a simple foreach() loop, but there are probably several ways of achieving this.
This uses array_flip() on the $keys, so that you end up with another associative array, then use array_intersect_key() with the big array first.
$final_array = array_intersect_key($big_array, array_flip($keys));
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
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],
// ...
));
if i have the following array:
array(
'var1' => 123,
'var2' => 234,
'var3' => 345
);
I would like to extract specific parts of this to build a new array i.e. var1 and var3.
The result i would be looking for is:
array(
'var1' => 123,
'var3' => 345
);
The example posted is very stripped down, in reality the array has a much larger number of keys and I am looking to extract a larger number of key and also some keys may or may not be present.
Is there a built in php function to do this?
Edit:
The keys to be extracted will be hardcoded as an array in the class i..e $this->keysToExtract
$result = array_intersect_key($yourarray,array_flip(array('var1','var3')));
So, with your edit:
$result = array_intersect_key($yourarray,array_flip($this->keysToExtract));
You don't need a built in function to do this, try this :
$this->keysToExtract = array('var1', 'var3'); // The keys you wish to transfer to the new array
// For each record in your initial array
foreach ($firstArray as $key => $value)
{
// If the key (ex : 'var1') is part of the desired keys
if (in_array($key, $this->keysToExtract)
{
$finalArray[$key] = $value; // Add to the new array
}
}
var_dump($finalArray);
Note that this is most likely the most efficient way to do this.
Let's say I have an array
$array = array(
'username' => "username",
'location' => "location",
'other' => "other");
This array can hold data for many users, so there could be different values for each 'username', 'location', and 'other' fields. How can I use in_array() or another function to determine if a specific username exists in the array already? Because what if a user has a username like "nyc" and a location of "nyc" and I do
in_array("nyc", $array);
How exactly should something like this be approached?
Thank you.
To achieve something that I think is what you want, you can make an array of associative arrays that have the same keys.
<?php
// This syntax will work only on PHP 5.4
$a=[["name"=>"john","age"=>25],["name"=>"philip","age"=>110]];
print_r(array_filter($a, function($item) {return $item["name"] === "john"; }));
?>
Outputs:
Array
(
[0] => Array
(
[name] => john
[age] => 25
)
)
If you just wanted to know if a person named John was in the list, you can just use sizeof/count on the returned array.
This will allow you to have any number of duplicates, and you don't need to specify any keys. Check out the functions: array_filter, array_reduce, array_map. With all of these, you can process your list using closures like in my example above.
Instead of using associative arrays in your array, you could have objects too. Objects are more heavyweight, and need initialization and stuff, so it is grotesque for using them for tiny static (hardcoded) lists. But they may come handy when your data structures grow and you want to make sure every list item has a certain property (the constructor of the class could ensure that everything is initialized). But the good thing is that filter, reduce and map would still work. The "$item" would then be your object.
$users = array( 'user_id' => array('username' => "username",
'location' => "location",
'other' => "other");
user_id is their NUMBER user_id
So you then call $users['####']['username'];
IE:
$users = array( '1' => array('username' => 'Jim',
'location' => 'Florida',
'other' => "other"),
'2' => array('username' => 'Jane',
'location' => 'Maryland',
'other' => "Grapes"));
Then use array_keys() to search for their user_id