PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, March 7, 2022

[FIXED] Convert Strings that hold only an Number into INT

 March 07, 2022     arrays, php     No comments   

Issue

I got an array full of items, all are string. But many of the items should be INT.

I got:

$myArray = [
    'id' => '123',
    'title' => 'Hello World',
    'count' => '333'
];

I want:

$myArray = [
    'id' => 123,
    'title' => 'Hello World',
    'count' => 333
];

I tried:

foreach ($myArray as $key => $value) {
    if($value == (int)$value) {
        $myArray[$key] = (int)$value;
    }
}
  • $value == (int)$value is always true and kills my title
  • $value === (int)$value is always false my id and count are still string
  • is_int($value) is always false my id and count are still string

And the I run out of ideas :-/

I'm on PHP 7.1.19 (cli)


Solution

Try i's_numeric' It will find whether a variable is a number or a numeric string http://php.net/manual/en/function.is-numeric.php



Answered By - manu
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing