I want to store 2 dependent values in this array:
["STEP5"]=> array(1) {
["OPTIONS"]=>
array(2) {
[0]=>
string(4) "opt2"
[1]=>
string(4) "opt3"
} }
Option fields (opt1, opt2,...) can have an extra field input. So how to store that in my array ? Thanx a lot.
You make each element of the OPTIONS array into an array itself. This would be better illustrated as:
["STEP5"] => array(1) {
["OPTIONS"] => array(2) {
[0] => array(2) {
["TEXT"] => "opt2"
["INPUT"] => "input data"
}
[1] => array(1) {
["TEXT"] => "opt3"
// No input data here
}
}
}
Then you can access the fields as:
$foo["STEP5"]["OPTIONS"][0]["TEXT"]
$foo["STEP5"]["OPTIONS"][0]["INPUT"]
$foo["STEP5"]["OPTIONS"][1]["TEXT"]
You don't have to have the "INPUT" field for every single option, but you can add it if you need to. Also, you might consider using objects to accomplish this task as they provide lots of useful functionality.
Related
We upload a lot of photos to WordPress. We do not use them all though. The unused ones remain "attached" to the post it's not in which makes it hard to go back and delete it.
I've build 2 arrays. One array contains all of the images from the post content ($postImages). The other array contains all of the images WordPress database have attached to the post ($mediaImages).
I'm trying to identify which images appear in the database, but not in the post. This way I can detach the image from within the database.
I'm using array_diff to compare the two arrays, however it's not showing the odd man out. It appears to be showing the matches.
Here's my arrays:
Call this one $postImages:
var_dump($postImages);
array(3) {
[2]=>
string(64) "http://mywebsite.com/wp-content/uploads/2013/11/photoblog1.jpg"
[0]=>
string(64) "http://mywebsite.com/wp-content/uploads/2013/11/photoblog2.jpg"
[1]=>
string(64) "http://mywebsite.com/wp-content/uploads/2013/11/photoblog3.jpg"
}
And $mediaImages:
var_dump($mediaImages);
array(4) {
[1]=>
array(1) {
[0]=>
string(64) "http://mywebsite.com/wp-content/uploads/2013/11/photoblog1.jpg"
}
[2]=>
array(1) {
[0]=>
string(64) "http://mywebsite.com/wp-content/uploads/2013/11/photoblog2.jpg"
}
[0]=>
array(1) {
[0]=>
string(64) "http://mywebsite.com/wp-content/uploads/2013/11/photoblog3.jpg"
}
[3]=>
array(1) {
[0]=>
string(62) "http://mywebsite.com/wp-content/uploads/2013/12/IMG_0069.jpg"
}
}
Here is the output:
$matches = array_diff($postImages, $mediaImages);
print_r($matches);
Array
(
[2] => http://mywebsite.com/wp-content/uploads/2013/11/photoblog1.jpg
[0] => http://mywebsite.com/wp-content/uploads/2013/11/photoblog2.jpg
[1] => http://mywebsite.com/wp-content/uploads/2013/11/photoblog3.jpg
)
Expected output:
Array
(
[0] => http://mywebsite.com/wp-content/uploads/2013/12/IMG_0069.jpg
)
As Marc B pointed out in the comments, $mediaImages is an array of arrays of strings, while $postimages is just an array of strings.
You can use array_map() with a custom callback to create the $mediaImages array:
$mediaImages = array_map(function($item) {
return $item[0];
}, $mediaImages);
Also, note that you have the parameters for array_diff() backwards. The correct order is:
array_diff($arrayToCompareFrom , $arrayToCompareAgainst);
So to compare $postImages against $mediaImages, you'd need:
$matches = array_diff($mediaImages, $postImages);
print_r($matches);
Demo.
I have an array with multiple values and need to push a value into this array.
The orignial array looks like:
[0]=> array(2) { ["name"]=> string(17) "Name" ["id"]=> string(8) "134567" }
[1]=> array(2) { ["name"]=> string(13) "Name" ["id"]=> string(9) "123456" }
And I need to put these values into the above array:
$personal['id']
$personal['name']
How can this be done?
Use square bracket notation to append to the original array:
$original[] = $person;
Or if $person is more complex and you only want those two keys:
$original[] = array(
'name' => $personal['name'],
'id' => $personal['id']);
Assuming that the $personal array only contains id and name you could use array_push.
array_push($array, $personal);
Seems that the guy who gave me the answer deleted his answer,
But this I used to add values to
$originalArray[]['id'] = $personal['id'];
$originalArray[]['name'] = $personal['name'];
Anyway ++1 for you, thanks! :)
See if this works for you:
$yourarray[]=$personal;
I would like to push an associate array into another array but I an not sure how to go about it. At the minute I have the following:
$rate_info = array(
"hotel_rating" => $hotel->{'hotelRating'},
"room_rate" => $hotel->{'RoomRateDetailsList'}->{'RoomRateDetails'}->{'RateInfo'}->{'ChargeableRateInfo'}->{'#total'},
"currency" => $hotel->{'RoomRateDetailsList'}->{'RoomRateDetails'}->{'RateInfo'}->{'ChargeableRateInfo'}->{'#currencyCode'},
"deep_link" => $hotel->{'deepLink'}
);
array_push($hotel_array[$hotel->{'name'}]["offers"], "expedia" => $rate_info );
"Offers" is an array , all I want to do is add an key value with an array within in. Any ideas? All I seem to be getting at the minute is parse errors.
UPDATE
This is the output of the array so far
["offers"]=>
array(2) {
["LateRooms"]=>
array(4) {
["hotel_rating"]=>
int(4)
["room_rate"]=>
string(6) "225.06"
["currency"]=>
string(3) "USD"
}
[0]=>
string(4) "test"
}
As you can see instad of [0] I would like ["site"]=>array()
Thanks
Oliver
I'd do this for the array assignment:
$hotel_array[$hotel->name]['offers']['expedia'] = $rate_info;
Ensure your warnings are enabled, so you know arrays (and subarrays) have been set up before you use them.
Did you first do this?
$hotel_array[$hotel->{'name'}] = array();
And then you can do:
array_push($hotel_array[$hotel->{'name'}]["offers"], "expedia" => $rate_info );
How can I get the follow thing done.
The follow array I have:
array(2) { [0] => array(3) {
["id"] => string(1) "5"
["avatar"] => string(15) "4e0d886ee9ed3_n"
["username"] => string(5) "testuser1"}
[1] => array(3) {
["id"] => string(1) "1"
["avatar"] => string(15) "4e25bc58b6789_w"
["username"] => string(6) "testuser2"
}
}
I want to create a array with just one user in it, but it has to be random. It can be like
user with id=5, id=1 or a hole other user (when there are more users).
Did you try, something like:
$rand = array_rand($your_array);
http://fr2.php.net/manual/en/function.array-rand.php
Try using array_rand().
$randomKey = array_rand($yourArray);
$randomUserId = $yourArray[$randomKey]['id'];
Simple.
$rand_user = array_rand($your_array);
PHP Manual: array_rand
I have the following array:
array(2) {
[0] => array(3) {
["submission_id"] => int(28)
["date"] => string(22) "2010-10-18 15:55:33+02"
["user_id"] => int(12)
}
[1] => array(3) {
["submission_id"] => int(37)
["date"] => string(22) "2010-11-21 16:02:07+01"
["user_id"] => int(23)
}
I want to get only the user_id key values from this array. I could obviously loop over it, but I was wondering if there was a quicker way.
You could use array_map (might not be quicker though, as it will make a function call per array element):
function getUserId($a) {
return $a['user_id'];
}
$user_ids = array_map('getUserId', $array);
Apart from that, looping is the only way (array_map makes loop anyway).
You can access only the user_id values like this if you know the array index you want to access:
$arr = your array here..
echo $arr[0]['user_id'];
echo $arr[1]['user_id'];