Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have the following array in PHP:
$item_array = array("facebook/1377445751.jpg_t","twitter/1377446022.gif_s","flickr/1377531219.png_w","flickr/1377531219.jpg_t_w");
I am iterating through this array, removing values that don't match the following regex:
(facebook|twitter|flickr)\/(\d{10})\.(jpg|png|gif)_(t_w|t|s|w)
Here is my code:
foreach($item_array as $key => $item) {
if(!preg_match('/(facebook|twitter|flickr)\/(\d{10})\.(jpg|png|gif)_(t_w|t|s|w)/', $item)) {
unset($item_array[$key]);
}
}
However, when I then use var_dump() on $item_array, I obtain an empty array. Since the preg_match doesn't match any of the array values, I'm under the impression that the problem lies there, even though I've tested it multiple times with RegExr.
Any ideas on where I might be going wrong?
As you can see here (click), your regex works fine. Perhaps you are having an issue you have not stated that deals with using the result.
I suspect that you might be looking for the $matches parameter, which will store your results.
Use like so:
preg_match('/(facebook|twitter|flickr)\/(\d{10})\.(jpg|png|gif)_(t_w|t|s|w)/', $item, $matches);
var_dump($matches);
This is all working perfectly as you described. I cleaned it up a little bit, but the code is the same. You must have a different problem.
$item_array = [
"facebook/1377445751.jpg_t",
"twitter/1377446022.gif_s",
"flickr/1377531219.png_w",
"flickr/1377531219.jpg_t_w",
"other stuff that won't match!"
];
foreach($item_array as $key => $item) {
$foo = preg_match('/(facebook|twitter|flickr)\/(\d{10})\.(jpg|png|gif)_(t_w|t|s|w)/', $item);
if(!$foo) {
echo $item; //displays "other stuff that won't match"
unset($item_array[$key]);
}
}
var_dump($item_array); //non match is removed
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Trying to understand how I can take multi-line textbox data and then utilise it using PHP.
Within the textbox I want users to be able to write down the format "Website,URL" per line and then generate as a link.
A user would post multiple lines of text:
E.g
Microsoft,https://microsoft.com
Stackoverflow,https://stackoverflow.com
I understand that I would need to use the php explode to break down the line into an array but how can I seperate the data from within that array and then utilise each point of data in a list format? I've confused myself writing this, hoping someone can help!
Given this :
Microsoft,https://microsoft.com Stackoverflow,https://stackoverflow.com
Expected to got this :
array(
'Microsoft' => 'https://microsoft.com',
'Stackoverflow' => 'https://stackoverflow.com'
)
Code
$data = "Microsoft,https://microsoft.com
Stackoverflow,https://stackoverflow.com";
$result = array();
foreach(explode(' ', $data) as $values){
$key_value = explode(",", $values);
if (isset($key_value[0]) && isset($key_value[1]))
$result[$key_value[0]] = $key_value[1]; // Construct array with meaningful key
}
var_dump($result);
Result
array(2) { ["Microsoft"]=> string(21) "https://microsoft.com" [" Stackoverflow"]=> string(25) "https://stackoverflow.com" }
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm trying to check if a filename of an image contains "cover". Somehow this stopped working for me (Pretty sure it worked already). I copied the part of my function not working.
$name=array("_IMG8555.jpg", "_IMG7769.jpg", "_IMG8458.jpg", "Cover.jpg", "_IMG7184.jpg");
$cov=array("Cover.png","Cover.jpg","Cover.jpeg", "cover.png","cover.jpg","cover.jpeg");
This does not work for me:
print_r(array_search($cov, $name)); //Returns empty String
print_r($name[array_search($cov, $name)]); //Returns first element of the name Array
Also I added a test Strings to make sure this is not result the searched string is the same as the search value.
print_r($name[3]===$cov[1]); //Returns true(1)
Can anyone help? Why does this simple script not work?
I also tried using in_array() but this is not working either.
The array_search() function search an array for a value and returns the key
array_search(key_value,array)
Loop your $cov array and get one key at a time and check with $name array
foreach($cov as $i => $cov_s){
if(in_array($cov_s, $name)){
return $name[array_search($cov_s, $name)];
}
}
return $name[0];
Try this code.
$name=array("_IMG8555.jpg", "_IMG7769.jpg", "_IMG8458.jpg", "Cover.jpg", "_IMG7184.jpg");
$cov=array("Cover.png","Cover.jpg","Cover.jpeg", "cover.png","cover.jpg","cover.jpeg");
foreach($cov as $c){
if(array_search($c,$name)){
//Do your success function
return true;
}
else
return false;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Ok as per your suggestion I updated... By default Laravel returns JSON... I have set it to return an array but I am still getting the same row duplicated using:
$limits = array();
foreach($pieces as $coverage_limit){
$limits[] = coveragelimit::index($coverage_limit);
}
return json_encode($limits);
}
You're just overwriting $limits inside that foreach() loop. Perhaps you mean something more like
foreach($pieces as $coverage_limit){
$limits[] = coveragelimit::index($coverage_limit);
^^--- array push?
}
As well, you don't "implement" JSON instead of arrays. You work with NATIVE data structures, then encode that structure into JSON. JSON's a transport format, it's not something you should ever deal with natively.
the $limits array will hold the last value what coveragelimit::index() returns over the iterate, I would suggest a check on coveragelimit::index() return value if it falls with "Marc B"'s answer.
EDIT:
foreach($pieces as $key=>$coverage_limit) {
$limits[$key] = coveragelimit::index($coverage_limit);
}
or
foreach($pieces as $coverage_limit) {
array_push($limits, coveragelimit::index($coverage_limit));
}
both should returns the same as Marc B's answer
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to get strings from a long array value.
for example
$array[1]='this is a good day. The number:1, class:math';
$array[2]='this is a bad day. The number:2, class:english';
$array[3]='this is a fine day. The number:3, class:physics';
I want to get the number:1 or class:math strings out of the array.
I tried
echo array_search('number:1',$array);
but it gave me nothing. I was wondering if there are better ways to do this. Thanks a lot!
I guess you're searching for something like the following. Searching for a needle inside of values of arrays.
<?php
function array_search_inline($needle, $haystack) {
foreach ($haystack as $key => $value) {
if (strpos($value, $needle) !== false) {
return $key;
}
}
return false;
}
?>
Note: array_search simply compares the array's values and does not search inside of them.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am currently working on a php project where I am trying to loop through an array using a foreach. However, sometimes the array may only contain 1 item so when I try and do a foreach it fails as 1 item is just seen as a normal variable.
Is there a way that I can trick php into thinking that a 1 item array is actually an array and not just a variable so that I don't get this error.
Thanks for your help.
foreach will work fine with arrays of size 0,1 or bigger. I suspect your problem is, that the variable doesn't really contain an array, but some scalar value - in this case use something like
if (!is_array($var)) $var=array($var);
foreach ($var as $item) {
//...
}
if(is_array($arr))
$arr2=$arr
else
$arr2=array($arr)
and then you iterate over $arr2
I would recommend just using a standard for loop. It should work no matter what the length of the array is
for($i = 0, $l = count($myArray); $i < $l; $i+=1){
//code in here
}
But chances are you have an issue with your array to begin. Posting the structure would be helpful, or you should var_dump it to make sure it is indeed an array.