Here's the idea: a user enters his ZIP code.
Based on the inserted ZIP code, I get an array of ZIP codes (distance ordered).
Next I want to order an existing array of ZIP codes based on the distance ordered array.
So basically I have two arrays:
Array which should be ordered
array(2) {
[0]=>
string(4) "2018"
[1]=>
string(4) "2500"
}
Distance ordered array
array(247) {
[0]=>
string(4) "2000"
[1]=>
string(4) "2500"
[2]=>
string(4) "2050"
[2]=>
string(4) "2018"
In this example, my array (number 1) should be ordered like so: [0] => 2500, [1] => 2018
How can I manage this?
You could use array_intersect() to get only the values of the second array that are also in the first array. And as the function preserves the keys - and so the order -, you only have to renumber them.
$a1=array(2018,2500);
$a2=array(2000,2500,2050,2018);
$a3=array_intersect( $a2 , $a1 );
echo print_r($a3,true);
Result:
Array (
[1] => 2500
[3] => 2018 )
Related
I have to extract strings from a xml file. One particular value has been generated using json enconding.
Here is a exemple of what I can find:
<plus_details>
[["Neuf"],["Petite copropri\u00e9t\u00e9"],["Vue mer"]]
</plus_details>
I would like to extract the strings and display them inline and separated by commas, like this :
Neuf, Petite copropriété, Vue mer
I tried using json_decode function, but the only thing I can display is:
array(3) {
[0]=>
array(1) {
[0]=>
string(4) “Neuf”
}
[1]=>
array(1) {
[0]=>
string(20) “Petite copropriété”
}
[2]=>
array(1) {
[0]=>
string(7) “Vue mer”
}
}
Any help would be appreciated. Thanks.
Simple use a loop to go through your data. When you json_decode the string you provided us, you will end-up with an array like this :
Array
(
[0] => Array
(
[0] => Neuf
)
[1] => Array
(
[0] => Petite copropriété
)
[2] => Array
(
[0] => Vue mer
)
)
So in order to get your data you need to loop your array.
foreach(json_decode($json) as $data){
echo $data[0];
echo '<br>';
}
The output of the above code is:
Neuf
Petite copropriété
Vue mer
Array ( [0] => UK [1] => France [2] => USA ) in these array get only values
like array(UK, France, USA) am trying like below,
$expression=array_values(array('0' => 'UK', '1' => 'France', '2' => 'USA'));
var_dump($expression);
OUTPUT PLAN:
array(3) { [0]=> string(2) "UK" [1]=> string(6) "France" [2]=> string(3) "USA" }
Can i get my desired output?
Please read answer carefully.
$arr = array('UK', 'France', 'USA'); // It has 0 ,1 ,2 keys but you cannot see in the code`
But in browser you can see it.
array(3) { [0]=> string(2) "UK" [1]=> string(6) "France" [2]=> string(3) "USA" }
Just use loop to print each country
foreach($arr as $country){
echo $country."<br>";
}
You can understand it by below loop
foreach($arr as $k=>$country){
echo "$k => $country"."<br>"; // Here $k is key like 0,1,2..
}
If you have array like below:-
$arr = array('uk'=>'UK', 'france'=>'France', 'usa'=>'USA') // It has uk ,france ,usa keys
now array_values($arr) will give you output as below
array(3) { [0]=> string(2) "UK" [1]=> string(6) "France" [2]=> string(3) "USA" }
It will remove all keys and regenerate index of key from 0.
Refer below links to understand PHP array:-
Link1
Link2
Link3
Hope it will help you :)
You could just do:
$out = array();
foreach($old_array as $new_value) { //Where $old_array is the array you want to convert
array_push($out, $new_value);
}
All PHP arrays have an internal index whether you attach one or not. So in your example even if you created an array using $countries = array("USA", "UK", "France") it would still output with indexes.
You can however ignore the indexes when your looping through it and only work with the values using a foreach() loop.
An example of such a loop would be...
foreach($expression as $index => $country) {
echo($country . "<br />");
};
The above example will print each country on its own line. You can adapt it to suit whatever looping you need.
As a side note be aware that the index will commence from 0 so item 1 in the array will have an index of 0 and increment from there.
You have this array.
Array ( [0] => UK [1] => France [2] => USA )
you have key and value assigned to it.
if you make another array like
array('UK','France','USA')
It is an associative array. Its the same as above.
Read it for more info
If you want to do operation with the array you can use foreach and for or other array related functions. So, first of all say what are you trying to accomplish with this one.
$expression=array_values(array('0' => 'UK', '1' => 'France', '2' => 'USA'));
foreach($expression as $val) {
echo $val.",";
}
Hi I have a PHP array
$arr = array(10,2,3,0=>4,5,6);
echo "<pre>";
var_dump($arr);
my Expected result was
<pre>array(6) {
[0]=>
int(4)
[1]=>
int(10)
[2]=>
int(2)
[3]=>
int(3)
[4]=>
int(5)
[5]=>
int(6)
}
But my result is
<pre>array(5) {
[0]=>
int(4)
[1]=>
int(2)
[2]=>
int(3)
[3]=>
int(5)
[4]=>
int(6)
}
From the documentation it says
http://php.net/manual/en/language.types.array.php
Example #5 Keys not on all elements
<?php
$array = array(
"a",
"b",
6 => "c",
"d",
);
var_dump($array);
?>
array(4) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[6]=>
string(1) "c"
[7]=>
string(1) "d"
}
**here I can understand that no other element have the key 6 . and in my case i think because the first element also have the key 0 . it get removed .
But because we have added key 0 to another element externally , i feel that 10 should have key 1 . please explain this . thank you very much .
The example you cite does not match your actual use case. In it you are explicitly setting an array key which is not yet defined. In your example you are defining an array key which has already been defined. Thus in your situation you are overriding the original value with the new value you have set later in your array declaration.
I think that the answer to your problem is in example 2 on that page.
<?php
$array = array(
1 => "a",
"1" => "b",
1.5 => "c",
true => "d",
);
var_dump($array);
?>
The above example will output:
array(1) {
[1]=>
string(1) "d"
}
i.e. later entries in an array creation will over-write earlier entries. Because in the array you created the first element "10" is assigned the key 0, the later entry 0=>4 overwrites it. Therefore what you are seeing is the expected result from array creation in PHP.
The problem here is , You are defining the value for a key which is already assigned to a value. Let me explain it a bit.
When the PHP interpreter runs through the array, It assigns
[0] -> 10
[1] -> 2
[2] -> 3
then it sees [0] -> 4 ,
So it replace the [0] -> 10 with [0] -> 4
So on..
[3] -> 5
[4] -> 6
Hope you will understand.
I have a dialogue file that looks like so:
CHARACTER MOOD PROMPT RESPONSE TEXT LEVEL PATH
As you can see, everything is separated by spaces. The trick comes in when
PROMPT RESPONSE TEXT is supposed to be one header (read together) all heading groups are separated by no more than two spaces while each heading is separated by 3+ spaces. What I am trying to do is take this line and add it into an array much like this:
array(4) => {
[0]=> string(9) "CHARACTER",
[1]=> string(4) "MOOD",
[2]=> string(21) "PROMPT RESPONSE TEXT",
[3]=> string(5) "LEVEL",
[4]=> string(4) "PATH"
}
I am trying to use preg_split with the following regexp /\s\s\s+/ but it does nothing more than yield an empty array. I assume that the regexp would split if on any amount of spaces equal to or greater than 3. Is there something more to this?
You can use the following, this looks for whitespace ( at least 3 times )
$results = preg_split('/\s{3,}/', $text);
var_dump($results);
Output
array(5) {
[0]=> string(9) "CHARACTER"
[1]=> string(4) "MOOD"
[2]=> string(21) "PROMPT RESPONSE TEXT"
[3]=> string(5) "LEVEL"
[4]=> string(4) "PATH"
}
If you dont want to have the overhead of loading the regex engine you could use this
<?php
$t = 'CHARACTER MOOD PROMPT RESPONSE TEXT LEVEL PATH';
$u = explode(' ',$t);
print_r($u);
$new_u = array();
foreach( $u as $key => $val) {
if ($val != '') {
$new_u[] = trim($val);
}
}
print_r($new_u);
Results
Array
(
[0] => CHARACTER
[1] =>
[2] => MOOD
[3] =>
[4] =>
[5] => PROMPT RESPONSE TEXT
[6] =>
[7] => LEVEL
[8] =>
[9] =>
[10] => PATH
)
Array
(
[0] => CHARACTER
[1] => MOOD
[2] => PROMPT RESPONSE TEXT
[3] => LEVEL
[4] => PATH
)
I've two arrays array1 and array2 and I want to add all elements of array2 to the end of array1. array1 contains many items.
The keys are numeric and I don't want this syntax:
array1 = array1 + array2
or
array1 = SomeArrayFun(array1,array2)
As it takes away CPU times ( as array is created twice )
What I want is:
array1 . SomeAddFun(array2); // This will not create any new arrays
Is there any way to do it?
If you'd like to append data to an existing array you should se array_splice.
With the proper arguments you'll be able to insert/append the contents of $array2 into $array1, as in the below example.
$array1 = array (1,2,3);
$array2 = array (4,5,6);
array_splice ($array1, count ($array1), 0, $array2);
print_r ($array1);
output:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
)
You might use ArrayObject with the append function:
$arrayobj = new ArrayObject(array('first','second','third'));
$arrayobj->append('fourth');
Result:
object(ArrayObject)#1 (5) {
[0]=>
string(5) "first"
[1]=>
string(6) "second"
[2]=>
string(5) "third"
[3]=>
string(6) "fourth"
}
Don't know for appending arrays though, as they seem to be appended as a "subarray" and not as part of the whole.
Docs: http://www.php.net/manual/en/arrayobject.append.php