Jump to content

Manipulate a single value in an array


KevBurgess

Recommended Posts

Hi everyone,

so, if this looks like homework, it is because it is but I haven't got a clue where to start!

I have this array:

$name = array["Burgess, Kevin"];

As it is, this is the surname followed by the forename.  The task is to display it as "Kevin Burgess". 

At this point I don't want the whole answer, just a place to begin because I am struggling to find a single function or combination of functions that can help me on my way.

Ever grateful for any help..... :D

Regards,

Kev

Link to comment
Share on other sites

32 minutes ago, KevBurgess said:

$name = array["Burgess, Kevin"];

Hardly seems worth putting  a single value in an array variable. Also check out the syntax for declaring arrays. It should be either

$name = array("Burgess, Kevin");    // use (..)

// or

$name = ["Burgess, Kevin"]          // use [..]
Link to comment
Share on other sites

Hmmm....

You have been given (as you say) a task to complete.  Sounds like homework of a sort to me.

So - why have you been given this task?  I assume to help you in a learning process.  Why then is your attempt at learning simply to post to this forum (and others?) looking for the answers instead of doing your (hopefully) assigned reading?  As Cyberobot has said - What have you read?

If you don't think reading is a good thing or if you are one of the many wanna-be programmers I see on forums such as this who say that they do better with videos and such and don't learn well from reading(!), then let me suggest that programming is not for you.  Go back to video games or whatever keeps you occupied if you can't be bothered to learn how to read and experiment with a true learning process.  Every one of the languages I learned in my 40 years of IT involved reading a manual as well as other tutorials and class materials as well as hours of practice and thought.   If that doesn't interest you then you are on the wrong path.

 

(My day to be hard I think.)

Link to comment
Share on other sites

This assignment probably doesn't require a function, but you do need to look at Barand's post.

Given your original syntax mistake I have to also question the constitution of the original array, because it is vitally important to the solution.

This code gives you an array with a single element which happens to be the name as a string:

$name = array("Burgess, Kevin");

This code gives you an array with 2 string elements in it:

$name = array("Burgess", "Kevin");

There are also a few online PHP sandboxes you can use for small tests or explorations.  I tend to use this one:   https://3v4l.org/SSkpg

In the case of the single string, there is helpful function called explode.

In the case that it's meant to be an array with 2 elements, then you simply need to understand how to reference an element in a simple array by it's numeric index. See Phi11w's post.

Link to comment
Share on other sites

17 hours ago, Barand said:

Hardly seems worth putting  a single value in an array variable. Also check out the syntax for declaring arrays. It should be either


$name = array("Burgess, Kevin");    // use (..)

// or

$name = ["Burgess, Kevin"]          // use [..]

There are actually 10 values in the array that I will loop through using a foreach function.  I put a single value in there to demonstrate the format and what I aim to achieve.

Link to comment
Share on other sites

16 hours ago, ginerjm said:

Hmmm....

You have been given (as you say) a task to complete.  Sounds like homework of a sort to me.

So - why have you been given this task?  I assume to help you in a learning process.  Why then is your attempt at learning simply to post to this forum (and others?) looking for the answers instead of doing your (hopefully) assigned reading?  As Cyberobot has said - What have you read?

If you don't think reading is a good thing or if you are one of the many wanna-be programmers I see on forums such as this who say that they do better with videos and such and don't learn well from reading(!), then let me suggest that programming is not for you.  Go back to video games or whatever keeps you occupied if you can't be bothered to learn how to read and experiment with a true learning process.  Every one of the languages I learned in my 40 years of IT involved reading a manual as well as other tutorials and class materials as well as hours of practice and thought.   If that doesn't interest you then you are on the wrong path.

 

(My day to be hard I think.)

"Sounds like homework of a sort to me."

As I mentioned in my original post, it is indeed homework that I am struggling to find a starting point with.

 

"Why then is your attempt at learning simply to post to this forum (and others?) looking for the answers instead of doing your (hopefully) assigned reading?"

As I mentioned in my original post I am not looking for the answer.  I am looking for a starting point.  I have been looking through the various array functions available in PHP but am still getting to grips with terminology.  I am a DBA with 25 years of experience in Oracle, SQL Server, DB2 on iSeries and Exasol and am starting out in programming pretty late.  I know where to find DBA answers but as far as programming is concerned still trying to figure out what questions to ask.

 

"If you don't think reading is a good thing or if you are one of the many wanna-be programmers I see on forums such as this who say that they do better with videos and such and don't learn well from reading(!), then let me suggest that programming is not for you.  Go back to video games or whatever keeps you occupied if you can't be bothered to learn how to read and experiment with a true learning process.  Every one of the languages I learned in my 40 years of IT involved reading a manual as well as other tutorials and class materials as well as hours of practice and thought.   If that doesn't interest you then you are on the wrong path."

I do think reading is a good thing.  Unfortunately by not reading my original post you have shown that you however do not.

I do not play video games.  I am a serious IT professional learning PHP of a much larger learning curriculum in IT Management.

I do use vidoes to supplement my reading (real books by the way.....with paper and my side notes in them.....) and don't consider them bad

I can be bothered to learn and read and have invested 100's of Euros in books to do exactly that.

 

And finally......

If all you can do is insult serious profesional people that are simply looking for help then perhaps a forum is not for you.....

 

Regards,

Kevin

 

 

 

Link to comment
Share on other sites

14 hours ago, gizmola said:

This assignment probably doesn't require a function, but you do need to look at Barand's post.

Given your original syntax mistake I have to also question the constitution of the original array, because it is vitally important to the solution.

This code gives you an array with a single element which happens to be the name as a string:


$name = array("Burgess, Kevin");

This code gives you an array with 2 string elements in it:


$name = array("Burgess", "Kevin");

There are also a few online PHP sandboxes you can use for small tests or explorations.  I tend to use this one:   https://3v4l.org/SSkpg

In the case of the single string, there is helpful function called explode.

In the case that it's meant to be an array with 2 elements, then you simply need to understand how to reference an element in a simple array by it's numeric index. See Phi11w's post.

Hi Gizmola,

thanks for the hint.

The syntax as I posted it was indeed wrong and should have been......

$name = array("Burgess, Kevin");

This is then a single string element that needs the forename and surname swapped around and will eventually be part of a new array.  If it were two single elements I could have simply referenced the elements using their indexes ( [1], [0] ) to achieve the same result.

Solving the problem in SQL I would use REPLACE (to remove the comma), SUBSTRING (to extract each of the two items and put them into variables) and then a SELECT PARAM1 || PARAM2 to display the results.  The problem I am having is trying to perform the operation with PHP.

A whole new world for me.... :)

Regards,

Kev

Link to comment
Share on other sites

29 minutes ago, KevBurgess said:

There are actually 10 values in the array that I will loop through using a foreach function.  I put a single value in there to demonstrate the format and what I aim to achieve.

Sorry, I forgot to mention, this element is a single string with surname and forename and it is this string I am trying to manipulate.  Once I understand how to do it with one doing it with all becomes much easier.

Link to comment
Share on other sites

18 hours ago, cyberRobot said:

What have you tried?

At the moment, not much.  I am trying to find a function like INSTRING or SUBSTRING that allows me to extract parts of a string within an element in an array.

When I have code written I will post here.....

Link to comment
Share on other sites

Make sure you credit me and this forum to your professor/boss/superior. I reccomend you learn what this code is doing and not just copy/paste.

<?php
/* Code by benanamen - phpfreaks.com 
 * https://forums.phpfreaks.com/topic/307571-manipulate-a-single-value-in-an-array/
 */

$name = array("Burgess, Kevin", "Smith, John");
foreach ($name as $v){
$v = explode(", ", $v);
echo $v[1] . " " . $v[0] . "\n";    
}

 

Link to comment
Share on other sites

1 hour ago, benanamen said:

Make sure you credit me and this forum to your professor/boss/superior. I reccomend you learn what this code is doing and not just copy/paste.


<?php
/* Code by benanamen - phpfreaks.com 
 * https://forums.phpfreaks.com/topic/307571-manipulate-a-single-value-in-an-array/
 */

$name = array("Burgess, Kevin", "Smith, John");
foreach ($name as $v){
$v = explode(", ", $v);
echo $v[1] . " " . $v[0] . "\n";    
}

 

Hi Benanamen,

many thanks for the solution and of course, the code will be credited accordingly.

I do have one request though....

If you see me post a question then I am not looking for a complete solution, just a point at which I can start to find a solution myself.  The solution I come to I will post here for the benefit of others.

I am very much a programming amateur at the moment and am trying to learn how to code properly and not how to use code from others....that isn't my way..... :)

 

Regards,

Kev

Link to comment
Share on other sites

1 hour ago, KevBurgess said:

The solution I come to I will post here for the benefit of others.

What would you have us do if the solution you post is sub-optimal? For the benefit of others (and you) wouldn't we be obliged to post a better one?

EDIT:

A case in point is your multiplication table post :

where your posted solution uses while() loops.

  • Use for() loops when then the number of iterations is known.
  • Use while() loops when the number is unknown but the is a defineable end condition (such as end-of-file)

So this would be better:

    for ($row=1; $row<=10; $row++) {
        echo "<tr><th>$row</th>";
        for ($col=1; $col<=10; $col++) {
            echo "<td>" . $row * $col . "</td>";
        }
        echo "</tr>\n";
    }

 

Link to comment
Share on other sites

1 hour ago, Barand said:

What would you have us do if the solution you post is sub-optimal? For the benefit of others (and you) wouldn't we be obliged to post a better one?

EDIT:

A case in point is your multiplication table post :

where your posted solution uses while() loops.

  • Use for() loops when then the number of iterations is known.
  • Use while() loops when the number is unknown but the is a defineable end condition (such as end-of-file)

So this would be better:


    for ($row=1; $row<=10; $row++) {
        echo "<tr><th>$row</th>";
        for ($col=1; $col<=10; $col++) {
            echo "<td>" . $row * $col . "</td>";
        }
        echo "</tr>\n";
    }

 

If my solution is suboptimal then by all means correct it!  For me a forum such as this is one of the most useful tools for learning that there is because experience is a teacher and not simply facts read out of a book.

My point is that I (and I am sure many others like me) don't necessarily want the answer thrown at us because that isn't learning.  A nudge in the right direction (as in the suggestion offered by my multiplication table example) is prefferable so I work on a (good or not so good) solution and receive appropriate comments and nudges.

I appreciate everyones help and comments but I am still going through a quite challenging learning process at the moment that takes me in a completely different direction from PL-SQL and T-SQL and I need to beat my head into submission to make this learning process work..... :)

Link to comment
Share on other sites

Also, there are often multiple solutions to any problem, here is another

<?php

 function revname( $str ) {
   return implode( ' ', array_reverse( explode( ',', $str ) ) );
 }
 
 echo implode( '<br>', array_map( revname, array( 'Burgess, Kevin', 'Smith, John' ) ) );

 // Kevin Burges<br>John Smith

?>

 

Link to comment
Share on other sites

3 hours ago, alpine said:

Also, there are often multiple solutions to any problem, here is another

And as @Barand basically just posted, sometimes you need to be spanked when you make a mistake. You forgot to quote the function name which would have gave you a warning if you had error reporting turned on (previously a notice <7.24). But nice example of a function. ?

 

<b>Warning</b>:  Use of undefined constant revname - assumed 'revname' (this will throw an Error in a future version of PHP) in <b>[...][...]</b> on line <b>7</b>

 

Link to comment
Share on other sites

1 hour ago, alpine said:

Sorry, im on two computers and tested it from one and then just typed it in the forumpost from another, so it glitched naturally.

No big fuzz, but brilliant that the post cannot be edited.

Yeah, I would prefer to be able to fix typos. On other forums I can but not this one. How about it admins, can you grant edit privileges to some of us after the timeout?

Link to comment
Share on other sites

19 hours ago, alpine said:

Also, there are often multiple solutions to any problem, here is another


<?php

 function revname( $str ) {
   return implode( ' ', array_reverse( explode( ',', $str ) ) );
 }
 
 echo implode( '<br>', array_map( revname, array( 'Burgess, Kevin', 'Smith, John' ) ) );

 // Kevin Burges<br>John Smith

?>

 

Hi Alpine,

many thanks for your input.  It is a cool solution I hadn't considered....

 

Regards,

Kev

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.