Jump to content

Recommended Posts

Hi there,

 

I am working  on an associative array:

 

The array is:

$currentIds
//when we do 
print_r($currentIds); 

//it returns value 41 and 42.
Array ( [0] => [1] => 41 [2] => 42 [3] => ) 

Which is OK but I have to use these two values in an SQL query but they are in Array forms. I need to convert them to integer to be able to use them in SQL query.

 

Please let me know how I can convert this Array to Integer so that i can use the integer values for my SQL statement.

 

Thank you :)

 

All comments and feedback are always welcomed :)

 

Cheers!

Link to comment
https://forums.phpfreaks.com/topic/214268-converting-array-to-integer-values/
Share on other sites

What does var_dump($currentIds); look like?

 

For Array of:

 

Array ( [0] => [1] => 41 [2] => 42 [3] => )  

 

It looks like this:

  0 => string '' (length=0)
  1 => string '41' (length=2)
  2 => string '42' (length=2)
  3 => string '' (length=0)

 

It just need to grab the integer 41 and 42 from this array.  :-\

$first  = (int) $currentIds[1];
$second = (int) $currentIds[2];

 

Thanks for the reply... the thing is that, I cannot predict beforehand the number of items in this array.

 

So I can't write :

 

(int) $currentIds[1];

(int) $currentIds[2]; etc..

 

Sometimes there will be 3 items sometimes 5 in a single Array.

 

Because once I get the number, I will be using it in a join statement. So I have to get the individual number from the Array.

 

What other method do you suggest to get the number from the Array?

 

Thanks!

 

Hi

 

Sounds like you need to use a foreach to loop around the array.

 

$sql = "SELECT * FROM SomeTable WHERE ";
$ParmArray = array();
foreach($currentIds AS $field=>$value)
{
$ParmArray[] = " (someField = $field AND someOtherField = '$value') ";
}
$sql .= implode(' OR ',$ParmArray);

 

Something like that maybe (you will need to catch the situation where $currentIds is empty).

 

All the best

 

Keith

Please describe your problem more clearly and thoroughly. Making up new restrictions, constraints or related-issues isn't conducive to helping us to help you out.

 

Thanks for the reply bro.

 

My scenario is that, I have a Discounts table which has Product ID in the ProductID field and having values as:

 

ProductID

|2|5|7|14|19|

 

This indicates which products the discounts apply to.

 

I use the function (which returns an array of products ) to get the product ID from Discounts Table:

 

$ProductInDiscounts = getProduct(array('startDate' =>  date('Y-m-d 00:00:00'), 'startDate' =>  date('Y-m-d 00:00:00'))

foreach ($ProductInDiscounts as $pid)
    {
//I use explode function coz the ProductIDs are stored concatenated with | sign      
$currentIds = explode("|", $pid['productId']);

//Now when once we do
print_r($currentIds ); 

//We get in return
//Array ( [0] => [1] => 41 [2] => 42 [3] => ) 

 

Now I just need to get these Products IDs (41 and 42) from this array so that I can use them in SQL join statements.

 

I hope you get the picture of what I am trying to accomplish here.

 

Thanks :)

 

 

 

Hi

 

Sounds like you need to use a foreach to loop around the array.

 

$sql = "SELECT * FROM SomeTable WHERE ";
$ParmArray = array();
foreach($currentIds AS $field=>$value)
{
$ParmArray[] = " (someField = $field AND someOtherField = '$value') ";
}
$sql .= implode(' OR ',$ParmArray);

 

Something like that maybe (you will need to catch the situation where $currentIds is empty).

 

All the best

 

Keith

 

Thanks for the reply.. But I am already using the foreach loop. See my previous post.

 

And I am able to get the values as:

 

Array ( [0] => [1] => 41 [2] => 42 [3] => )

 

Now I just need to grab 41 and 42 from this array. And the thing is that we cannot predict the number of items in an array as there can be more than one products in the discounts table.

 

:-\

 

Your biggest problem is you shouldn't be storng values in a pipe delimtered string in the first place. Give each id its own row.

 

Anyway, if you want to leave you data broken as is, you can (and should) still clean up your code so you don't get empty values within your array. Is that your actual problem?

 

$currentIds = explode("|", trim($pid['productId'], '|'));

Thanks for the reply.. But I am already using the foreach loop. See my previous post.

 

And I am able to get the values as:

 

Array ( [0] => [1] => 41 [2] => 42 [3] => )

 

Now I just need to grab 41 and 42 from this array. And the thing is that we cannot predict the number of items in an array as there can be more than one products in the discounts table.

 

:-\

 

You can embed a foreach within a foreach. So you explode the pipe delimited string to an array and then foreach around that array.

 

<?php

 

$ProductInDiscounts = getProduct(array('startDate' =>  date('Y-m-d 00:00:00'), 'startDate' =>  date('Y-m-d 00:00:00'))

 

foreach ($ProductInDiscounts as $pid)
{
//I use explode function coz the ProductIDs are stored concatenated with | sign      
$currentIds = explode("|", $pid['productId']);
foreach($currentIds AS $anId)
{
	// Process that single id
}
}

?>

 

However, why is getProduct bringing back an array of pipe delimited items? It would seem more logical to bring back a single array of items, or a single pipe delimited string or just an array of arrays.

 

All the best

 

Keith

Your biggest problem is you shouldn't be storng values in a pipe delimtered string in the first place. Give each id its own row.

 

Anyway, if you want to leave you data broken as is, you can (and should) still clean up your code so you don't get empty values within your array. Is that your actual problem?

 

$currentIds = explode("|", trim($pid['productId'], '|'));

 

Thanks for the reply and we have over hundreds of lines of codes using this method. (using Pipe | )  to store value in one row.

 

You are right, it should have been in separate row.

 

Anyways, the code you shared returns:

 

Array ([0] => 41 [1] => 42 )

 

And I just need from the array these 2 numbers (41 and 42) so that I could use them in my SQL Statement, which is:

 

SELECT * FROM discounts LEFT JOIN products p on p.productId = discounts.productId*

 

*Here we are going to put the ProductID array values once we are able to grab the number from the array.

 

Thank you!

 

I think we're getting close to it. :)

 

 

You can embed a foreach within a foreach. So you explode the pipe delimited string to an array and then foreach around that array.

 

<?php

 

$ProductInDiscounts = getProduct(array('startDate' =>  date('Y-m-d 00:00:00'), 'startDate' =>  date('Y-m-d 00:00:00'))

 

foreach ($ProductInDiscounts as $pid)
{
//I use explode function coz the ProductIDs are stored concatenated with | sign      
$currentIds = explode("|", $pid['productId']);
foreach($currentIds AS $anId)
{
	// Process that single id
}
}

?>

 

However, why is getProduct bringing back an array of pipe delimited items? It would seem more logical to bring back a single array of items, or a single pipe delimited string or just an array of arrays.

 

All the best

 

Keith

 

Thank you your solution does return the Integer. I am very happy to see it :)

 

We are again one step closer to the solution.

 

Now that I've the integer from the Array...

 

Where shall I place my SQL query? Inside the innermost foreach loop or the outer loop?

 

Because when I am placing the SQL inside the loop it does the looping and picks up the last ProductID from the foreach loop.

 

Ok here is what I get when I type:

 

 

    foreach ($ProductID2Explode as $pexp)

    {
        $currentIds   = explode("|", trim($pexp['productId'], '|'));

        foreach($currentIds AS $anId)

        {
            echo "<br/>Innermost Loop ProductID IS: ";
            print_r($anId); 
        }

}

 

And the output I get is:

 

Innermost Loop ProductID IS: 41
Innermost Loop ProductID IS: 42
Innermost Loop ProductID IS: 42
Innermost Loop ProductID IS: 39
Innermost Loop ProductID IS: 37
Innermost Loop ProductID IS: 39

 

Which is perfect. But when I put the SQL Query inside it i.e.

 

SELECT * FROM discounts LEFT JOIN products p on p.productId = $anId

 

The query runs fine but it only picks up the final number (i.e. 39) after the looping.

 

Where shall I place the query to be able to read all the numbers returned from the foreach loop.

 

Thank you!

Hi

 

Keeping the embedded foreach like this:-

 

<?php

$ProductInDiscounts = getProduct(array('startDate' =>  date('Y-m-d 00:00:00'), 'startDate' =>  date('Y-m-d 00:00:00'))

foreach ($ProductInDiscounts as $pid)
{
//I use explode function coz the ProductIDs are stored concatenated with | sign      
$currentIds = explode("|", $pid['productId']);
foreach($currentIds AS $anId)
{
	$FullArray[] = $anId;
}
}

$sql = 'SELECT * FROM discounts d LEFT JOIN products p on d.productId = p.productId WHERE p.ProductId IN ('.implode(',',$FullArray).')';

?>

 

However you should also be able to do this

 

<?php

$ProductInDiscounts = getProduct(array('startDate' =>  date('Y-m-d 00:00:00'), 'startDate' =>  date('Y-m-d 00:00:00'))

$currentIds = array();
foreach ($ProductInDiscounts as $pid)
{
//I use explode function coz the ProductIDs are stored concatenated with | sign      
$currentIds = array_merge($currentIds,explode("|", $pid['productId']));
}

$sql = 'SELECT * FROM discounts d LEFT JOIN products p on d.productId = p.productId WHERE p.ProductId IN ('.implode(',',$currentIds).')';

?>

 

All the best

 

Keith

Hi

 

Keeping the embedded foreach like this:-

 

<?php

$ProductInDiscounts = getProduct(array('startDate' =>  date('Y-m-d 00:00:00'), 'startDate' =>  date('Y-m-d 00:00:00'))

foreach ($ProductInDiscounts as $pid)
{
//I use explode function coz the ProductIDs are stored concatenated with | sign      
$currentIds = explode("|", $pid['productId']);
foreach($currentIds AS $anId)
{
	$FullArray[] = $anId;
}
}

$sql = 'SELECT * FROM discounts d LEFT JOIN products p on d.productId = p.productId WHERE p.ProductId IN ('.implode(',',$FullArray).')';

?>

 

However you should also be able to do this

 

<?php

$ProductInDiscounts = getProduct(array('startDate' =>  date('Y-m-d 00:00:00'), 'startDate' =>  date('Y-m-d 00:00:00'))

$currentIds = array();
foreach ($ProductInDiscounts as $pid)
{
//I use explode function coz the ProductIDs are stored concatenated with | sign      
$currentIds = array_merge($currentIds,explode("|", $pid['productId']));
}

$sql = 'SELECT * FROM discounts d LEFT JOIN products p on d.productId = p.productId WHERE p.ProductId IN ('.implode(',',$currentIds).')';

?>

 

All the best

 

Keith

 

Thanks for the reply bro!

 

Your first suggestion for using the:

 

$FullArray[] = $anId;

 

in the innermost loop does return all the productIds. :)

 

But when I use it inside my SQL query, that is:

 

 

$sql = 'SELECT * FROM discounts d LEFT JOIN products p on d.productId = p.productId WHERE p.ProductId IN ('.implode(',',$FullArray).')';

 

It renders the query as:

 

  select * FROM discounts d LEFT JOIN products p on d.productId = p.productId WHERE p.productId IN ('.implode(',',Array).')' 

 

Are we missing something here. :)

 

Hi

 

Double check the quotes are of the right kind and match. Can't anything obvious otherwise.

 

All the best

 

Keith

 

Thanks for the reply.

 

I tried to remove the comas I even removed the whole implode function and put the $FullArray directly in the query like:

 

SELECT * FROM discounts d LEFT JOIN products p on d.productId = p.productId WHERE p.ProductId IN $FullArray;

 

And it is still rendering it as:

SELECT * FROM discounts d LEFT JOIN products p on d.productId = p.productId WHERE p.ProductId IN Array ;

 

Oh dear... this is the very last step to the task. I hope we can find the way... :)

 

Do we need to do anything additional with  $FullArray variable for its value to be rendered properly in the query?

Hi

 

It has put array because it is an array.

 

The error you are getting is what I would expect if you had

 

$sql = "SELECT * FROM discounts d LEFT JOIN products p on d.productId = p.productId WHERE p.ProductId IN ('.implode(',',$currentIds).')";

 

instead of

 

$sql = 'SELECT * FROM discounts d LEFT JOIN products p on d.productId = p.productId WHERE p.ProductId IN ('.implode(',',$currentIds).')';

 

All the best

 

Keith

Hi

 

It has put array because it is an array.

 

The error you are getting is what I would expect if you had

 

$sql = "SELECT * FROM discounts d LEFT JOIN products p on d.productId = p.productId WHERE p.ProductId IN ('.implode(',',$currentIds).')";

 

instead of

 

$sql = 'SELECT * FROM discounts d LEFT JOIN products p on d.productId = p.productId WHERE p.ProductId IN ('.implode(',',$currentIds).')';

 

All the best

 

Keith

 

You are right. it has to be in double-quotes and I'm already enclosing it within double-quotes.

 

Just don't know why it is rendering it as Array instead of its values.

 

Even though the array has all the values in it.

 

when I do the

 

print_r($currentIds);

 

and it return the whole list of values: i.e.

 

Array ( [0] => 32 [1] => 14 [2] => 17 [3] => 26 [4] => 0 [5] => 55 [6] => 56 [7] => 55 [8] => 56 [9] => 40 [10] => 41 [11] => 42 [12] => 42 [13] => 39 [14] => 37 [15] => 39 [16] => 14 ) 

 

But when put in the SQL Query within double-qoutes with or without implode function this array variable just returns 'Array' instead of the list.

 

Whatelse do you think we can do to show the array value in the SQL Query.

 

Thanks! :)

Hi

 

No, it can be in either double quotes or single quotes AS LONG as the ones at the end match those either side of the implode.

 

Using double quotes at the ends and single quotes either side of the implode means that the function name (ie implode) is just treated as a bit of the strong, which the array variable is just treated as an array within a string and is put at array

 

Either of the following should be OK

 

$sql = 'SELECT * FROM discounts d LEFT JOIN products p on d.productId = p.productId WHERE p.ProductId IN ('.implode(',',$currentIds).')';

$sql = "SELECT * FROM discounts d LEFT JOIN products p on d.productId = p.productId WHERE p.ProductId IN (".implode(',',$currentIds).")";

 

The following will not work

$sql = 'SELECT * FROM discounts d LEFT JOIN products p on d.productId = p.productId WHERE p.ProductId IN (".implode(',',$currentIds).")';

$sql = "SELECT * FROM discounts d LEFT JOIN products p on d.productId = p.productId WHERE p.ProductId IN ('.implode(',',$currentIds).')";

 

All the best

 

Keith

Hi

 

No, it can be in either double quotes or single quotes AS LONG as the ones at the end match those either side of the implode.

 

Using double quotes at the ends and single quotes either side of the implode means that the function name (ie implode) is just treated as a bit of the strong, which the array variable is just treated as an array within a string and is put at array

 

Either of the following should be OK

 

$sql = 'SELECT * FROM discounts d LEFT JOIN products p on d.productId = p.productId WHERE p.ProductId IN ('.implode(',',$currentIds).')';

$sql = "SELECT * FROM discounts d LEFT JOIN products p on d.productId = p.productId WHERE p.ProductId IN (".implode(',',$currentIds).")";

 

The following will not work

$sql = 'SELECT * FROM discounts d LEFT JOIN products p on d.productId = p.productId WHERE p.ProductId IN (".implode(',',$currentIds).")';

$sql = "SELECT * FROM discounts d LEFT JOIN products p on d.productId = p.productId WHERE p.ProductId IN ('.implode(',',$currentIds).')";

 

All the best

 

Keith

 

Wow it worked like a charm! I think there is a reason why they call you ... GURU  :D

 

Way to go.

 

You are the man bro!!!  8)

Use array_filter to remove empty values.

 

implode(',', array_filter($currentIds))

 

Yes thank you so much for your input. It does remove the zeros and null values. :)

 

Is there a way to remove duplicate values in the array?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.